|
7 | 7 | /// A Dart library for building UI using ReactJS.
|
8 | 8 | library react;
|
9 | 9 |
|
| 10 | +import 'package:collection/equality.dart'; |
10 | 11 | import 'package:meta/meta.dart';
|
11 | 12 | import 'package:react/react_client/bridge.dart';
|
12 | 13 | import 'package:react/src/prop_validator.dart';
|
@@ -1230,6 +1231,28 @@ abstract class Component2 implements Component {
|
1230 | 1231 | _initProps(props) {}
|
1231 | 1232 | }
|
1232 | 1233 |
|
| 1234 | +/// Top-level ReactJS [PureComponent class](https://reactjs.org/docs/react-api.html#reactpurecomponent) |
| 1235 | +abstract class PureComponent extends Component2 { |
| 1236 | + @mustCallSuper |
| 1237 | + @override |
| 1238 | + bool shouldComponentUpdate(Map nextProps, Map nextState) { |
| 1239 | + return !PureComponent._shallowPropsEqual(props, nextProps) || !PureComponent._shallowStateEqual(state, nextState); |
| 1240 | + } |
| 1241 | + |
| 1242 | + static bool _shallowPropsEqual(Map map1, Map map2) { |
| 1243 | + // Does this work, or does props.children always make this return false? |
| 1244 | + return MapEquality().equals( |
| 1245 | + Map.of(map1)..remove('key')..remove('ref')..remove('children'), |
| 1246 | + Map.of(map2)..remove('key')..remove('ref')..remove('children'), |
| 1247 | + ) && |
| 1248 | + ListEquality().equals(map1['children'], map2['children']); |
| 1249 | + } |
| 1250 | + |
| 1251 | + static bool _shallowStateEqual(Map map1, Map map2) { |
| 1252 | + return MapEquality().equals(map1, map2); |
| 1253 | + } |
| 1254 | +} |
| 1255 | + |
1233 | 1256 | /// Mixin that enforces consistent typing of the `snapshot` parameter
|
1234 | 1257 | /// returned by [Component2.getSnapshotBeforeUpdate]
|
1235 | 1258 | /// and passed into [Component2.componentDidUpdate].
|
|
0 commit comments