Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix(compat): upgrade RangeInput lifecycle (#2289)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored and Haroenv committed Jun 27, 2019
1 parent 69a7f53 commit 110b1af
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
21 changes: 5 additions & 16 deletions packages/react-instantsearch-dom/src/components/RangeInput.js
Expand Up @@ -32,24 +32,13 @@ export class RawRangeInput extends Component {
this.state = this.normalizeStateForRendering(props);
}

componentWillReceiveProps(nextProps) {
// @TODO: Render, worst case in Derived State

// In react@16.0.0 the call to setState on the inputs trigger this lifecycle hook
// because the context has changed (for react). I don't think that the bug is related
// to react because I failed to reproduce it with a simple hierarchy of components.
// The workaround here is to check the differences between previous & next props in order
// to avoid to override current state when values are not yet refined. In the react documentation,
// they DON'T categorically say that setState never run componentWillReceiveProps.
// see: https://reactjs.org/docs/react-component.html#componentwillreceiveprops

componentDidUpdate(prevProps) {
if (
nextProps.canRefine &&
(this.props.canRefine !== nextProps.canRefine ||
this.props.currentRefinement.min !== nextProps.currentRefinement.min ||
this.props.currentRefinement.max !== nextProps.currentRefinement.max)
this.props.canRefine &&
(prevProps.currentRefinement.min !== this.props.currentRefinement.min ||
prevProps.currentRefinement.max !== this.props.currentRefinement.max)
) {
this.setState(this.normalizeStateForRendering(nextProps));
this.setState(this.normalizeStateForRendering(this.props));
}
}

Expand Down
Expand Up @@ -167,7 +167,7 @@ describe('RawRangeInput', () => {
expect(component).toMatchSnapshot();
});

describe('willReceiveProps', () => {
describe('didUpdate', () => {
it('expect to update state when props have changed ', () => {
const props = {
canRefine: false,
Expand Down

0 comments on commit 110b1af

Please sign in to comment.