How to remember page status before react jump
React implements the method of remembering the page status before jumping: 1. Monitor path changes, and update lastPath and currentPath to the redux store when the path changes; 2. When leaving page A, save the page status to In the redux store; 3. If the lastPath in the redux store is equal to the path of page B, it is considered that A is returned to the restored state by B, otherwise it will not be restored.
The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.
How to remember the page status before jumping in react?
React page returns to retain the last state
Requirements
Page A jump Go to page B and then return to page A. Page A needs to restore the state before leaving;
There are multiple entrances to page A and page B. Jump to page A from other pages. Page A does not restore state.
##Design
- Listen to path changes and update lastPath and currentPath to the redux store when the path changes;
- When leaving page A, save the page status to the redux store;
- When entering page A, if the lastPath in the redux store is equal to page B path, it is considered that A is returned to the restored state by B, otherwise it will not be restored.
Implementation
The project uses the react-router dva library, and the implementation part will involve related technologies. Monitor path changes, monitor path changes through history, and record lastPath and currentPath. Here, DVA subscriptions are used to subscribe to history, and when the path changes, the path information is synchronized to the state.const model = { namespace: "global", state: { pathName: { last: "", current: "" }, }, reducers: { setPathName(state: any, { pathName }: any) { state.pathName.last = state.pathName.current; state.pathName.current = pathName; }, effects: { }, subscriptions: { setup({ history, dispatch }: any) { return history.listen(({ pathName }: any) => { dispatch({ type: "global/setPathName", pathName }); }); } } };
componentWillUnmount() { const { dispatch } = this.props; const { activeKey } = this.state; dispatch({ type: "projectInfo/setProjectInfoPage", payload: { activeKey } }); }
state = { activeKey: pathToRegexp(PagePath.B).exec(pathName.last) ? activeKey : "" };
Other solutions
Judge whether page A is returned by page B: add state when page B returns, history.push({ pathname: path, state: {from } });, enter the A page and judge whether to return from the B page according to the state. But when B has multiple entrances, you need to know the source of the page when returning, otherwise you cannot return, and the logic is slightly complicated and error-prone.Summary
This article proposes a solution for page return to retain the last state, which is suitable for situations where the page has multiple entrances and exits. This solution uses the method of monitoring history changes and recording the last page address, thus providing a basis for whether to restore the state. Recommended learning: "react video tutorial"
The above is the detailed content of How to remember page status before react jump. 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











PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

Title: Implementation method of page jump in 3 seconds: PHP Programming Guide In web development, page jump is a common operation. Generally, we use meta tags in HTML or JavaScript methods to jump to pages. However, in some specific cases, we need to perform page jumps on the server side. This article will introduce how to use PHP programming to implement a function that automatically jumps to a specified page within 3 seconds, and will also give specific code examples. The basic principle of page jump using PHP. PHP is a kind of

Title: PHP Programming Tips: How to Jump to a Web Page within 3 Seconds In web development, we often encounter situations where we need to automatically jump to another page within a certain period of time. This article will introduce how to use PHP to implement programming techniques to jump to a page within 3 seconds, and provide specific code examples. First of all, the basic principle of page jump is realized through the Location field in the HTTP response header. By setting this field, the browser can automatically jump to the specified page. Below is a simple example demonstrating how to use P

PHP is a programming language widely used in website development, and page jumps and carrying POST data are common requirements in website development. This article will introduce how to implement PHP page jump and carry POST data, including specific code examples. In PHP, page jumps are generally implemented through the header function. If you need to carry POST data during the jump process, you can do it through the following steps: First, create a page containing a form, where the user fills in the information and clicks the submit button. Acti in the form

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

Introduction to troubleshooting and solving techniques for PHP login jump issues: In web development, implementing the user login function is a very common operation. However, sometimes users cannot jump to the specified page correctly after logging in. This may be because there are some common problems that affect the implementation of the jump. This article will introduce some troubleshooting and solving techniques for PHP login jump problems, and provide specific code examples to help readers better understand and solve such problems. Problem 1: Unable to jump correctly after logging in Problem description: The user should jump to the specified page after logging in.

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.
