Home > Article > Web Front-end > Changing parameters in Url without refreshing in React-Router
This time I will bring you how to change the parameters of the Url in React-Router without refreshing. What are the things to pay attention to when changing the parameters of the Url in React-Router without refreshing? The following is a practical case. Let’s take a look. take a look.
ProblemI found a problem when writing the page today, that is, using the Url parameter passing function in React Router, like this :
export class MainRouter extends React.Component { render() { return ( <BrowserRouter> <Switch> ... <Route exact path={'/channel/:channelId'} component={ChannelPerPage}/> ... </Switch> </BrowserRouter> ); } }
According to the official documentation, you can use
this.props.match.params
in the ChannelPerPage component to get the value of the url parameter, but I found that if you only change the parameters in the url under this url For some changes, such as when the channelId changes from 1 to 2, the page will not be refreshed.
SolutionAfter consulting the information, I found that the root cause is that the change of props does not cause the component to be re-rendered, only the state Only changes will cause the component to be re-rendered, and the url parameters belong to props, so changing the url parameters will not cause the component to be re-rendered.
Later I discovered that there is a overridable method in the React component
componentWillReceiveProps(nextProps) { ... }
This method can be overridden in the React component. This method will be called when the props change, so you can Use this method to obtain nextProps, and modify the state content in this method so that the component can be re-rendered.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
vue2.0 Detailed explanation of the steps to implement real-time retrieval and update of the input boxJS to implement a simple shopping cart function Code analysisThe above is the detailed content of Changing parameters in Url without refreshing in React-Router. For more information, please follow other related articles on the PHP Chinese website!