Home Web Front-end JS Tutorial How to understand redux

How to understand redux

Sep 28, 2017 am 09:05 AM
redux how understand

Redux Principle

A company has three businesses: logistics (actionType), e-commerce (actionType), and advertising (actionType) , the funds earned from the three businesses are uniformly recorded in the company's financial system (state). One day, the e-commerce business sold 1 million worth of goods (actions) on the company's e-commerce platform. The funds earned were sent to the business's financial department in the form of invoices (dispatch). The financial department calculated the earnings through its own business blocks. The profit obtained (reducer) is then updated to the financial system (state).

Core principle: Through an event action, the results are sent to the reducer through dispatch. In the reducer, different business processes are performed on the data according to different actionTypes, and then the final results are updated into the state tree.

Since several of my bosses keep a close eye on the company's funds and must always pay attention to changes in funds, they set up an event (subscribe) to send a text message to notify them (listener) when there is a change in funds.

Principle: Redux has a built-in listeners array that allows external subscriptions to state data change events. When the data in the state tree changes, all listening events will be notified.

API explanation


function createStore(reducer, preloadedState, enhancer)
Copy after login

In the createStore method, the entire redux environment is initialized. preloadedState serves as the initial value of the state tree. This method returns the open interface of redux, and the state of redux can only be operated through the returned api.

CreateStore returns the returned API:


##

 return {
    dispatch,
    subscribe,
    getState,
    replaceReducer,
    [$$observable]: observable
  }
Copy after login

store.getState: Returns the state object currently maintained by redux;

store.subscribe : You can register subscription events through this interface, that is, when the state of redux is accessed (regardless of whether the data in the state is modified or not), redux will traverse all registered events.


function subscribe(listener) {
    if (typeof listener !== 'function') {
      throw new Error('Expected listener to be a function.')
    }

    let isSubscribed = true

    ensureCanMutateNextListeners()
    nextListeners.push(listener)

    return function unsubscribe() {
      if (!isSubscribed) {
        return
      }

      isSubscribed = false

      ensureCanMutateNextListeners()
      const index = nextListeners.indexOf(listener)
      nextListeners.splice(index, 1)
    }
  }
Copy after login

Store.dispatch: After the event action is run, push the results to the reducer through dispatch. The result of the action must be an ordinary js object, and must contain a type attribute. In the reducer, the data can be processed differently based on the type, and then updated to the corresponding state.

After the reducer, all listening events will be traversed regardless of whether the data has changed or not.


function dispatch(action) {
    if (!isPlainObject(action)) {
      throw new Error(
        'Actions must be plain objects. ' +
        'Use custom middleware for async actions.'
      )
    }

    if (typeof action.type === 'undefined') {
      throw new Error(
        'Actions may not have an undefined "type" property. ' +
        'Have you misspelled a constant?'
      )
    }

    if (isDispatching) {
      throw new Error('Reducers may not dispatch actions.')
    }

    try {
      isDispatching = true
      currentState = currentReducer(currentState, action)
    } finally {
      isDispatching = false
    }

    const listeners = currentListeners = nextListeners
    for (let i = 0; i < listeners.length; i++) {
      const listener = listeners[i]
      listener()
    }

    return action
  }
Copy after login
The entire single-page application only needs to call the createStore method once, and then ensure that the initialized object is globally available, so as to achieve unified management of data through redux.

The above is the detailed content of How to understand redux. 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 Article

Hot Article

Hot Article Tags

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 correctly understand value passing in PHP How to correctly understand value passing in PHP Mar 08, 2024 pm 03:30 PM

How to correctly understand value passing in PHP

In-depth understanding of how to use Linux pipelines In-depth understanding of how to use Linux pipelines Feb 21, 2024 am 09:57 AM

In-depth understanding of how to use Linux pipelines

Is there a future for employment in clinical pharmacy at Harbin Medical University? (What are the employment prospects for clinical pharmacy at Harbin Medical University?) Is there a future for employment in clinical pharmacy at Harbin Medical University? (What are the employment prospects for clinical pharmacy at Harbin Medical University?) Jan 02, 2024 pm 08:54 PM

Is there a future for employment in clinical pharmacy at Harbin Medical University? (What are the employment prospects for clinical pharmacy at Harbin Medical University?)

In-depth understanding of the strings.Split function in Go language documentation In-depth understanding of the strings.Split function in Go language documentation Nov 04, 2023 pm 01:14 PM

In-depth understanding of the strings.Split function in Go language documentation

React Redux Tutorial: How to use Redux to manage front-end state React Redux Tutorial: How to use Redux to manage front-end state Sep 26, 2023 am 11:33 AM

React Redux Tutorial: How to use Redux to manage front-end state

In-depth understanding of Boolean types in MySQL In-depth understanding of Boolean types in MySQL Mar 15, 2024 pm 05:30 PM

In-depth understanding of Boolean types in MySQL

Best practices for building scalable web applications using Go and Redux Best practices for building scalable web applications using Go and Redux Jun 18, 2023 pm 01:59 PM

Best practices for building scalable web applications using Go and Redux

Explain in simple terms: Thoroughly understand the working principle of Go language range Explain in simple terms: Thoroughly understand the working principle of Go language range Mar 12, 2024 pm 02:18 PM

Explain in simple terms: Thoroughly understand the working principle of Go language range

See all articles