Analysis: React-Router triggers render twice when routing jumps
Problem: When React-Router jumps, render is triggered twice, causing the page to be rendered repeatedly.
Reason: react-router ^3.x.x used in the project. When react-router jumps, the value of this.props.location.action will have two states. Both states will trigger render. So the page is rendered twice.
1. When the Link is clicked, this.props.location.action=PUSH, 2. When the browser moves forward or backward, this.props.location.action=POP.
So when the Link is clicked, the status is PUSH first, then the browser moves forward and backward, and the status changes to POP.
Solution: In the routing layer, use the react periodic function shouldComponentUpdate (students who are not familiar with the life cycle, please check the information separately) to judge whether this.props.location.action is worth it. Determine whether the value is PUSH or POP according to the actual needs of the project.
I chose POP because some requirements in the project require the use of window.location.hash='xxxxxxxx'. In this case, PUSH cannot be triggered, so the routing jump will fail.
1 |
|
Note: Facebook official said that this situation is a BUG of react-router, which has been fixed in ^4.x.x.
The above contents are all what I encountered during actual project development. Everyone encounters different BUGs, so please let me know. Thanks!
The above is the detailed content of Analysis: React-Router triggers render twice when routing jumps. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Technical solution: First write a drop-down box component. First, we first write a component to display the content of the drop-down box. The component name starts with: Select.vue Welfare Mall Saas Platform Activity Customized Rendering Component We want to render this component on the web page. The operation should be like this: when the mouse moves to the product service, render the drop-down box component as a component instance in appropriate location on the page. In vue3, rendering a Vonde, the core logic is as follows: import{createVNode,h,render,VNode}from'vue'importcomponentfrom"./component.

Why does event bubbling trigger twice? Event bubbling (Event Bubbling) means that in the DOM, when an element triggers an event (such as a click event), the event will bubble up from the element to the parent element until it bubbles to the top-level document object. . Event bubbling is part of the DOM event model, which allows developers to bind event listeners to parent elements, so that when child elements trigger events, the events can be captured and processed through the bubbling mechanism. However, sometimes developers encounter events that bubble up and trigger twice.

The world of cryptocurrencies is always in flux, with new tokens capturing the attention of seasoned investors looking for the next big opportunity.

Render means rendering and is a drawing term. Rendering is the last process of CG, and it is also the stage that finally makes the image conform to the 3D scene. Rendering is called Render in English, and some people will call it shading, but generally Shade is called For shading, Render is called rendering.

Why does event bubbling happen twice in a row? Event bubbling is an important concept in web development. It means that when an event is triggered in a nested HTML element, the event will bubble up from the innermost element to the outermost element. This process can sometimes cause confusion. One common problem is that event bubbling occurs twice in a row. In order to better understand why event bubbling occurs twice in a row, let's first look at a code example:

With the Skyline, HMD Global is set to unveil a mid-range smartphone in the style of the Nokia Lumia 920 on July 10. According to the latest information from the leaker @smashx_60, the Lumia design will soon also be used for a tablet, which will be c

Render Coin: An investment opportunity worthy of long-term holding Render Coin is a cryptocurrency based on the Ethereum blockchain and is designed to pay for rendering services on the decentralized rendering network Render. The goal is to address the high cost and inefficiency of traditional rendering solutions and provide artists and creators with an affordable and convenient rendering option. The advantage of Render coin is decentralization: Render coin is based on the Ethereum blockchain and has decentralized characteristics, avoiding the single point of failure and high costs of centralized rendering service providers. High efficiency: Render coins use distributed rendering technology to allocate rendering tasks to idle GPUs around the world, greatly improving rendering efficiency. Low cost: Render coin reduces the cost of rendering by eliminating the middleman.

Methods for react-router to implement forward and backward: 1. Implement routing forward through "goback(){this.props.history.goBack()}"; 2. Through "go(){this.props.history.go(-1) ) }" to implement routing fallback.
