M
M
Maksim B2018-07-21 18:24:45
React
Maksim B, 2018-07-21 18:24:45

How to properly link location.search and state?

Let's say there is a search bar with a Find button, if I click it, I do history.push('/search?q=' + this.state.search)
And now how to track and process the transition Back to qs.parse(location.search). q went back to the state?
The main condition is that this.state.search must be in the query part of the url.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-07-23
@Sundagy

You can take a react-router, and set a new state for the component, when changing props, at the time of componentWillReceiveProps (getDerivedStateFromProps). Although here it is not necessary to write in the state as a whole, since you have a URL address string and there is a state. It is relevant, there is no need to duplicate.
If without react-router, then look in this tutorial for the paragraph " train Router to respond to address changes " (there is not react-router, but just a made Router component)
The "back" button works if you use browserHistory / hashHistory out of the box.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question