What are the ways to change Url parameters in React-Router without refreshing the page?

php中世界最好的语言
Release: 2018-05-12 10:17:45
Original
3796 people have browsed it

This time I will bring you what are the ways to change the Url parameters in React-Router without refreshing the page. What are the ways to change the Url parameters in React-Router without refreshing the page?What are the precautions?, the following is the actual combat Let’s take a look at the case.

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 (   ...  ...   ); } }
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:

Detailed explanation of JS callback function usage cases

Detailed explanation of common addition, deletion, modification and query operations of JS DOM elements

The above is the detailed content of What are the ways to change Url parameters 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
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!