Skip to content

Commit ce372c4

Browse files
Implement PureComponent
1 parent 9324ee9 commit ce372c4

File tree

4 files changed

+446
-0
lines changed

4 files changed

+446
-0
lines changed

lib/react.dart

+23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/// A Dart library for building UI using ReactJS.
88
library react;
99

10+
import 'package:collection/equality.dart';
1011
import 'package:meta/meta.dart';
1112
import 'package:react/react_client/bridge.dart';
1213
import 'package:react/src/prop_validator.dart';
@@ -1230,6 +1231,28 @@ abstract class Component2 implements Component {
12301231
_initProps(props) {}
12311232
}
12321233

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+
12331256
/// Mixin that enforces consistent typing of the `snapshot` parameter
12341257
/// returned by [Component2.getSnapshotBeforeUpdate]
12351258
/// and passed into [Component2.componentDidUpdate].

pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ homepage: https://github.com/cleandart/react-dart
1313
environment:
1414
sdk: '>=2.4.0 <3.0.0'
1515
dependencies:
16+
collection: ^1.14.12
1617
js: ^0.6.0
1718
meta: ^1.1.6
1819
dev_dependencies:

0 commit comments

Comments
 (0)