Preface: Continuing from the previous article, Front-end State Management (Part 1), I didn’t expect so many readers to pay so much attention to it. Thank you all for your support and suggestions. I am just expressing my personal opinions and some of my own thoughts, which may not be comprehensive enough. The example of using Vue is just a guide and Vue also has a high level of attention. Some friends want to hear other solutions besides Vuex. Today we will start with Redux and gradually expand (just like the title). 1. Redux As a member of the The basic operations of Redux are roughly as follows:
1.1. Store (librarian) import { createStore } from 'redux' const store = createStore(reducer); 1.2. State (Book) For Use import { createStore } from 'redux' const store = createStore(reducer) store.getState() 1.3. Action (Book Borrowing List) What if you want to borrow a book? Of course, you need to submit a book borrowing list to the administrator. Then the user cannot access const action = { type: 'click', info: 'Submit borrowing list' } 1.4、store.dispatch (submit borrowing list) store.dispatch(action) 1.5. Reducer (Packaging Books) After const reducer = (state, action) => { return `action.info: ${state}` // => Submit borrowing list: Dream of the Red Chamber} store.subscribe (receive books) Once the State changes, store.subscribe() will listen to it and automatically update the View. const unsubscribe = store.subscribe(() => { render() { // Update the view } }) // You can also unsubscribe (listen) unsubscribe() summary: I believe that students who have just come into contact with Although it is still impossible to make everything deterministic in
As for why we should do this, I have already mentioned it in the previous article. Its importance lies in: facilitating application testing, error diagnosis and 2. Purpose of state managementIn fact, the most common scenario for most programmers to use Redux is to return to page B from page A and need to save the status of page B. If the project is not large, would using But unfortunately there is no Let’s use the library as an example. Now there is a library management system. When you jump from the list page (list) to Assuming that the technology stack you use is ( //KeepAlive.js export default function keepAliveWrapper() { return function keepAlive(WrappedComponent) { return class KeepAlive extends WrappedComponent { // ps constructor(props) { super(props) // do something ... } componentDidMount() { const { keepAlive: { fieldsValue }, } = this.context // do something ... super.componentDidMount() } render() { // do something ... return super.render() } } } } Here is why we need to inherit the original component If the conventional writing method returns a class component ( If // main.jsx root component import React from 'react' const appContext = React.createContext() class App extends React.Component { constructor(props) { super(props) this.state = { keepAlive: {}, // Cache object isCache: false, // Whether to cache fieldsValue: {} // Cache form value } } componentDidMount() { // Initialize const keepAlive = { isCache: this.state.isCache, toggle: this.toggleCache.bind(this), fieldsValue: this.state.fieldsValue, } this.setState({ keepAlive }) } // Here is a method to clear the state to prevent rendering warnings (you can't set fields before render ...) // For example, list1 => list1/detail => list2 needs to put the jump in the following callback and clear the status toggleCache(isCache = false, payload, callback) { const { fieldsValue = null } = payload const keepAlive = { isCache, fieldsValue, toggle: this.toggleCache.bind(this), } const fn = typeof callback === 'function' ? callback() : void 0 this.setState( { keepAlive, }, () => { fn } ) } render() { const { keepAlive } = this.state <appContext.Provider value={{ keepAlive }}> // your routes... </appContext.Provider> } } As for why we don’t use // import React from 'react' when using it on the page import keepAlive from '../keepAlive' // keepAlive needs to be placed closest to the original component @keepAlive() class App extends React.Component { constructor(props){ super(props) this.state = { // init something... } } componentDidMount() { // do something... if(this.context.keepAlive.fieldsValue) { const { tableList } = this.context.keepAlive.fieldsValue console.log('Cached:',tableList) // Cache: ['1', '2'] } } // View details detail = () => { this.context.keepAlive.fieldsValue = { tableList: ['1', '2'] } // jump... } // When you need to jump across the first-level route, such as list1 => list1/detail (the following method should be in the details page) => list2, you need to handle the warning toList2 = () => { this.context.keepAlive.toggle(false, {}, () => { // jump... }) } } In the above, decorator writing is used. To put it simply, you need to configure the following Configure in { "compilerOptions": { "experimentalDecorators": true }, "exclude": [ "node_modules", "dist" ] } Configure in .babelrc: { "plugins": [ "@babel/plugin-proposal-decorators", { "legacy": true } ] } The above method is more suitable for the scenario just mentioned (returning from page A to page B requires saving the status of page B). Some people say that you might as well use Summarize: This is the end of this article about front-end status management. For more content related to front-end status management, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! Review of the previous article: A brief discussion on front-end state management (Part 1) You may also be interested in:
|
<<: MySQL select results to perform update example tutorial
>>: Nginx tp3.2.3 404 problem solution
This article describes the commonly used MySQL fu...
like LIKE requires the entire data to match, whil...
Table of contents Variable Scope The concept of c...
Preface Today, a developer gave me feedback that ...
1. MacVlan There are many solutions to achieve cr...
MySQL is a relational database management system....
Overview The prototype pattern refers to the type...
Nginx, pronounced "engine x," is an ope...
The specific code is as follows: <div id="...
Most of this article refers to other tutorials on...
Let's take a look at the dynamic splicing of ...
nginx installation Ensure that the virtual machin...
Table of contents React upload file display progr...
I'm very happy. When encountering this proble...
background On mobile devices, caching between pag...