Skip to content

Commit

Permalink
Migrate away from deprecated componentWillReceiveProps
Browse files Browse the repository at this point in the history
This implementation is not ideal, but it works. The preferred alternative, getDerivedStateFromProps, does not have access to the previous properties and thus is insufficient for comparing new nodes to the old ones. This comparison is needed to determine whether the expensive node flattening method should be called or skipped.

Alternatively, the previous props could be stored in the state and thus be recalled in getDerivedStateFromProps.
  • Loading branch information
jakezatecky committed Jan 22, 2019
1 parent 90fd8e0 commit d9a1b88
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/js/CheckboxTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,18 @@ class CheckboxTree extends React.Component {
this.onCollapseAll = this.onCollapseAll.bind(this);
}

componentWillReceiveProps({ nodes, checked, expanded }) {
shouldComponentUpdate({ nodes, checked, expanded }) {
// Since flattening nodes is an expensive task, only update the state when there is a change
if (!isEqual(this.props.nodes, nodes)) {
this.state.model.flattenNodes(nodes);
}

this.state.model.deserializeLists({ checked, expanded });

// Always update. We are hijacking this method to act as a hybrid between
// getDerivedStateFromProps and legacy componentWillReceiveProps, updating the internals
// of a state variable.
return true;
}

onCheck(nodeInfo) {
Expand Down

0 comments on commit d9a1b88

Please sign in to comment.