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

Commit

Permalink
fix(react-router-example): Properly update search query when using br…
Browse files Browse the repository at this point in the history
…owser navigation (#604)

This example properly updates the browser history on changes, but if you navigate using the browser back and next buttons, the search query doesn't change to match. This addition of a `componentWillReceiveProps` lifecycle method fixes that issue.
  • Loading branch information
lsanwick authored and samouss committed Nov 14, 2017
1 parent 2e50b10 commit 9ee6600
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ const createURL = state => `?${qs.stringify(state)}`;

const searchStateToUrl = (props, searchState) =>
searchState ? `${props.location.pathname}${createURL(searchState)}` : '';
const urlToSearchState = location => qs.parse(location.search.slice(1));

class App extends Component {
constructor(props) {
super(props);
this.state = { searchState: qs.parse(props.location.search.slice(1)) };
this.state = { searchState: urlToSearchState(props.location) };
this.onSearchStateChange = this.onSearchStateChange.bind(this);
}

componentWillReceiveProps(props) {
if (props.location !== this.props.location) {
this.setState({ searchState: urlToSearchState(props.location) });
}
}

onSearchStateChange(searchState) {
clearTimeout(this.debouncedSetState);
this.debouncedSetState = setTimeout(() => {
Expand Down

0 comments on commit 9ee6600

Please sign in to comment.