Home Web Front-end Front-end Q&A How to remember page status before react jump

How to remember page status before react jump

Jan 06, 2023 pm 01:59 PM
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.

How to remember page status before react jump

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 });
      });
    }
  }
};
Copy after login

Synchronize the status to the redux store when the page is unloaded, for example:

componentWillUnmount() {
    const { dispatch } = this.props;
    const { activeKey } = this.state;
    dispatch({
      type: "projectInfo/setProjectInfoPage",
      payload: { activeKey }
    });
  }
Copy after login

When the page is reloaded, for example:

state = {
    activeKey: pathToRegexp(PagePath.B).exec(pathName.last) ? activeKey : ""
  };
Copy after login

pathToRegexp comes from the path-to-regexp library, used Route matching, used here to determine whether the previous page is page B.

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!

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 build a reliable messaging app with React and RabbitMQ How to build a reliable messaging app with React and RabbitMQ Sep 28, 2023 pm 08:24 PM

How to build a reliable messaging application with React and RabbitMQ Introduction: Modern applications need to support reliable messaging to achieve features such as real-time updates and data synchronization. React is a popular JavaScript library for building user interfaces, while RabbitMQ is a reliable messaging middleware. This article will introduce how to combine React and RabbitMQ to build a reliable messaging application, and provide specific code examples. RabbitMQ overview:

React Router User Guide: How to implement front-end routing control React Router User Guide: How to implement front-end routing control Sep 29, 2023 pm 05:45 PM

ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

How to implement PHP code to jump to a specified page How to implement PHP code to jump to a specified page Mar 07, 2024 pm 02:18 PM

When writing a website or application, you often encounter the need to jump to a specific page. In PHP, we can achieve page jump through several methods. Below I will demonstrate three common jump methods for you, including using the header() function, using JavaScript code, and using meta tags. Using the header() function The header() function is a function used in PHP to send original HTTP header information. This function can be used in combination when implementing page jumps. Below is a

PHP, Vue and React: How to choose the most suitable front-end framework? PHP, Vue and React: How to choose the most suitable front-end framework? Mar 15, 2024 pm 05:48 PM

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.

How to implement page jump in 3 seconds: PHP Programming Guide How to implement page jump in 3 seconds: PHP Programming Guide Mar 25, 2024 am 10:42 AM

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

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

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.

How to close jump shopping app How to close jump shopping app Nov 29, 2023 pm 05:30 PM

Methods to close the jump shopping app: 1. Turn off the jump function within the app; 2. Change browser settings; 3. Uninstall updates or reinstall the app. Detailed introduction: 1. Turn off the jump function in the app, open the shopping app, click on the product you want to buy on the homepage or search results page, and after entering the product details page, do not directly click "Buy Now" or similar buttons, but First click the "More" or "Settings" icon in the upper right corner of the page. In the pop-up menu, find "Turn off jump" or a similar option, click it, confirm to turn off the jump function, etc.

PHP programming skills: How to jump to the web page within 3 seconds PHP programming skills: How to jump to the web page within 3 seconds Mar 24, 2024 am 09:18 AM

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

See all articles