The solution to the page not refreshing after the route changes after react jumps

The solution to the page not refreshing after the route changes after react jumps

question

There seem to be many reasons for this problem. My problem is that the URL with parameters cannot be refreshed. In router 5.0 version, using the withRouter associated component to jump to the page is as follows

insert image description here

Routing Code

insert image description here

Solution

Adding a key to the top-level element of the routing component increases the recognition of the route, because ordinary jumps are identified based on the path, but when the path carries parameters, the route cannot be accurately identified. However, when jumping to a page, a key will be added to the localtion object for each address. Print as follows

 // Component mounting componentDidMount() {
    console.log(this.props.location);
  }

insert image description here

We can accurately locate the route by binding this key to the top-level element of the route.

 render() {
    return (
      {/*This is the key*/}
      <div key={this.props.location.key}>
          <Switch>
            <Route exact path="/" component={Home} />
            <Route exact path="/products/:id" component={Products} />
            <Route exact path="/about" component={About} />
            <Route exact path="/solution" component={Solution} />
            <Route
              exact
              path="/solutionDetails/:id"
              component={SolutionDetails}
            />
            <Route exact path="/download" component={Download} />
            <Route path="/about" component={Download} />
            <Route exact path="/details/:id" component={Details} />
            <Route path="/contact" component={Contact} />
            <Route component={ErrorPage} />
          </Switch>
      </div>
    );
  }

However, you may find that this.props is an empty object {}. That may be because you did not use withRouter to associate the component. Just associate it. Note that app.js cannot be associated, withrouter can only be associated with routing components or subcomponents of app.js

import React, { Component } from "react";
import { withRouter } from "react-router";

class routers extends Component {
 /**
  * Life cycle function */
 // Component mounting componentDidMount() {
   console.log(this.props.location);
 }
 render() {
   return (
     <div key={this.props.location.key}>
     </div>
   );
 }
}
export default withRouter(routers);

This is the end of this article about the solution to the problem that the page does not refresh after the route changes after react jumps. For more related content about the page not refreshing after react jumps, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the solution to the problem that the page does not refresh when the Url parameter changes in React-Router
  • Solution to the 404 problem of react-router browserHistory refresh page

<<:  Data Structure - Tree (III): Multi-way Search Tree B-tree, B+ tree

>>:  Detailed explanation of MySQL from getting started to giving up - installation

Recommend

Nginx handles http request implementation process analysis

Nginx first decides which server{} block in the c...

How to Rename a Group of Files at Once on Linux

In Linux, we usually use the mv command to rename...

Example code for converting http to https using nginx

I am writing a small program recently. Because th...

About debugging CSS cross-browser style bugs

The first thing to do is to pick a good browser. ...

How to upgrade CentOS7 to CentOS8 (detailed steps)

This article uses a specific example to introduce...

List rendering instructions for efficient development of Vue front-end

v-for directive Speaking of lists, we have to men...

JavaScript to achieve digital clock effect

This article example shares the specific code of ...

Specific use of Docker anonymous mount and named mount

Table of contents Data volume Anonymous and named...

Vue project packaging and optimization implementation steps

Table of contents Packaging, launching and optimi...

React uses emotion to write CSS code

Table of contents Introduction: Installation of e...

What is ssh port forwarding? What's the use?

Table of contents Preface 1. Local port forwardin...

Analysis of the reasons why Vue3 uses Proxy to implement data monitoring

Vue data two-way binding principle, but this meth...

Solution to the ineffective global style of the mini program custom component

Table of contents Too long to read Component styl...