Home > Web Front-end > JS Tutorial > body text

How to handle the Url parameter in React-Router without refreshing the page

php中世界最好的语言
Release: 2018-05-31 10:01:39
Original
1560 people have browsed it

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={&#39;/channel/:channelId&#39;} component={ChannelPerPage}/>
          ...
        </Switch>
      </BrowserRouter>
    );
  }
}
Copy after login

According to the official documentation, you can use

this.props.match.params
Copy after login

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) {
 ...
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!