diff --git a/examples/react-router-v3/package.json b/examples/react-router-v3/package.json index f675a15aa1..a3716bfb04 100644 --- a/examples/react-router-v3/package.json +++ b/examples/react-router-v3/package.json @@ -21,7 +21,7 @@ "react": "16.8.6", "react-dom": "16.8.6", "react-instantsearch-dom": "5.7.0", - "react-router": "3.0.5" + "react-router": "3.2.1" }, "browserslist": [ ">0.2%", diff --git a/examples/react-router-v3/src/App.js b/examples/react-router-v3/src/App.js index b7a70f0d2b..ca8caf837d 100644 --- a/examples/react-router-v3/src/App.js +++ b/examples/react-router-v3/src/App.js @@ -22,41 +22,41 @@ const searchClient = algoliasearch( '6be0576ff61c053d5f9a3225e2a90f76' ); +const THRESHOLD = 700; +const createURL = state => `?${qs.stringify(state)}`; + class App extends Component { - constructor(props) { - super(props); - this.state = { searchState: { ...qs.parse(props.router.location.query) } }; - this.onSearchStateChange = this.onSearchStateChange.bind(this); - this.createURL = this.createURL.bind(this); - } + state = { + searchState: qs.parse(this.props.router.location.query), + }; - componentWillReceiveProps() { - // @TODO: derived state - this.setState({ searchState: qs.parse(this.props.router.location.query) }); - } + static getDerivedStateFromProps(props, state) { + const newSearchState = qs.parse(props.router.location.query); + + if (!isEqual(newSearchState, state.searchState)) { + return { + searchState: newSearchState, + }; + } - shouldComponentUpdate(nextProps, nextState) { - return !isEqual(this.state.searchState, nextState.searchState); + return null; } - onSearchStateChange(nextSearchState) { - const THRESHOLD = 700; + onSearchStateChange = nextSearchState => { const newPush = Date.now(); - this.setState({ lastPush: newPush, searchState: nextSearchState }); - if (this.state.lastPush && newPush - this.state.lastPush <= THRESHOLD) { - this.props.router.replace( - nextSearchState ? `?${qs.stringify(nextSearchState)}` : '' - ); - } else { - this.props.router.push( - nextSearchState ? `?${qs.stringify(nextSearchState)}` : '' - ); - } - } - createURL(state) { - return `?${qs.stringify(state)}`; - } + this.setState({ lastPush: newPush, searchState: nextSearchState }, () => { + if (this.state.lastPush && newPush - this.state.lastPush <= THRESHOLD) { + this.props.router.replace( + nextSearchState ? `?${qs.stringify(nextSearchState)}` : '' + ); + } else { + this.props.router.push( + nextSearchState ? `?${qs.stringify(nextSearchState)}` : '' + ); + } + }); + }; render() { return ( @@ -65,7 +65,7 @@ class App extends Component { indexName="instant_search" searchState={this.state.searchState} onSearchStateChange={this.onSearchStateChange} - createURL={this.createURL} + createURL={createURL} >
Hierarchical Menu
@@ -98,6 +99,7 @@ class App extends Component {Range Ratings