Home Web Front-end JS Tutorial Analysis: React-Router triggers render twice when routing jumps

Analysis: React-Router triggers render twice when routing jumps

Jul 22, 2017 pm 01:57 PM
react-router render twice

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

1 shouldComponentUpdate() {2         // POP 浏览器前进后退, PUSH 点击Link3         return this.props.location.action === "POP"4 }

Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement menu drop-down box in vue3 through render function How to implement menu drop-down box in vue3 through render function May 10, 2023 pm 04:28 PM

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? Why does event bubbling trigger twice? Feb 22, 2024 am 09:06 AM

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.

Mpeppe (MPEPE) Coin: A New Contender in the Cryptocurrency Market Attracting Investors from Render (RNDR) and Internet Computer (ICP) Mpeppe (MPEPE) Coin: A New Contender in the Cryptocurrency Market Attracting Investors from Render (RNDR) and Internet Computer (ICP) Sep 03, 2024 pm 02:03 PM

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

Does render mean rendering? Does render mean rendering? Feb 02, 2023 pm 02:52 PM

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 the event bubbling mechanism trigger twice? Why does the event bubbling mechanism trigger twice? Feb 25, 2024 am 09:24 AM

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:

HMD Slate Tab 5G leakes as mid-range tablet with Snapdragon 7s Gen 2, 10.6-inch display and Lumia design HMD Slate Tab 5G leakes as mid-range tablet with Snapdragon 7s Gen 2, 10.6-inch display and Lumia design Jun 18, 2024 pm 05:46 PM

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

Is Render coin worth holding for the long term? Is Render Coin worth investing in? Is Render coin worth holding for the long term? Is Render Coin worth investing in? Mar 06, 2024 am 08:31 AM

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.

How to implement forward and backward in react-router How to implement forward and backward in react-router Dec 20, 2022 pm 02:17 PM

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.

See all articles