This time I will show you how to handle the Url parameters in React-Router and the page will not refresh. How to handle the Url parameters in React-Router and the page will not refresh. What are the things to note?The following is a practical case. Let’s take a look. take a look.
Problem
I 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.
Solution
After 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:
How to package the vue project to the server
How to use webstorm to add *.vue files
The above is the detailed content of How to handle the Url parameter in React-Router without refreshing the page. For more information, please follow other related articles on the PHP Chinese website!