Skip to content

Commit

Permalink
Implement PureComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronlademann-wf committed Nov 21, 2019
1 parent 9324ee9 commit ce372c4
Show file tree
Hide file tree
Showing 4 changed files with 446 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/react.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/// A Dart library for building UI using ReactJS.
library react;

import 'package:collection/equality.dart';
import 'package:meta/meta.dart';
import 'package:react/react_client/bridge.dart';
import 'package:react/src/prop_validator.dart';
Expand Down Expand Up @@ -1230,6 +1231,28 @@ abstract class Component2 implements Component {
_initProps(props) {}
}

/// Top-level ReactJS [PureComponent class](https://reactjs.org/docs/react-api.html#reactpurecomponent)
abstract class PureComponent extends Component2 {
@mustCallSuper
@override
bool shouldComponentUpdate(Map nextProps, Map nextState) {
return !PureComponent._shallowPropsEqual(props, nextProps) || !PureComponent._shallowStateEqual(state, nextState);
}

static bool _shallowPropsEqual(Map map1, Map map2) {
// Does this work, or does props.children always make this return false?
return MapEquality().equals(
Map.of(map1)..remove('key')..remove('ref')..remove('children'),
Map.of(map2)..remove('key')..remove('ref')..remove('children'),
) &&
ListEquality().equals(map1['children'], map2['children']);
}

static bool _shallowStateEqual(Map map1, Map map2) {
return MapEquality().equals(map1, map2);
}
}

/// Mixin that enforces consistent typing of the `snapshot` parameter
/// returned by [Component2.getSnapshotBeforeUpdate]
/// and passed into [Component2.componentDidUpdate].
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ homepage: https://github.com/cleandart/react-dart
environment:
sdk: '>=2.4.0 <3.0.0'
dependencies:
collection: ^1.14.12
js: ^0.6.0
meta: ^1.1.6
dev_dependencies:
Expand Down
Loading

0 comments on commit ce372c4

Please sign in to comment.