Detailed explanation of the relationship between React and Redux

Detailed explanation of the relationship between React and Redux

Documentation: Redux Chinese Documentation

The official Redux documentation defines Redux as: a predictable JavaScript application state management container.

1. The relationship between redux and react

Redux does not only provide state management for react applications, it also supports other frameworks.

React is an abstraction layer for DOM (UI library), not a complete solution for web applications. Therefore, react is more complicated when it comes to data processing and communication between components.

For large and complex applications, these two aspects are precisely the most critical. Therefore, it is difficult to write large applications using only React.

Advantages of redux:

Centrally store and manage application status

When dealing with component communication issues, ignore the hierarchical relationship between components

Simplify communication between components in large and complex applications

The data flow is clear and it is easy to locate bugs

2. React multi-component sharing

Extract the states of all components and construct a centralized state tree by analogy with the React component tree. This state tree corresponds one-to-one with the React component tree, which is equivalent to stateful modeling of the React component tree:

├── src
   ├── store # redux directory, generally called store
   │ ├── index.js # Define and export the store. The reducer will be imported
   │ └── reducer # reducer function ├── App.js # root component, import Father and Uncle components 

1. Redux can ignore the component hierarchy

2. For the component system, redux is a third-party, global "variable"

3. Three core concepts of redux

Core concepts: store , action , reducer

1. store

Store is a warehouse, the core of Redux, integrating action and reducer, similar to the store of vuex

Features:

  • An application has only one store
  • Maintain the application status and obtain the status: store.getState re.getState()
  • When creating a store, receive the reducer as a parameter: const st store = createStore(reducer)
  • When initiating a status update, you need to dispatch action:store.dispatch(action)
import { createStore } from 'redux'
// Create a store
const store = createStore(reducer)

2. action

action is a js object with two properties:

type : Identifies the attribute, and its value is a string. Multiple types are separated by actions

payload : data attribute, optional. Indicates the data carried by this action

Features:

  • Just describe what to do
  • JS object, must have a type attribute to distinguish the type of action
  • Depending on the function, additional data can be carried to complete the corresponding function.
const action1 = { type:'addN', payload: 12 }
//store.dispatch(action1)
 
const action2 = { type:'add', payload: 1 }

3. Reducer (pure function)

effect:

1. Initialization state

2. Modify status

Modify state: return a new state based on the old state and action passed in

initState = 0
function reducer(state = initState, action) {
  return state
}

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • React and Redux array processing explanation
  • Understand the initial use of redux in react in one article
  • Detailed introduction to react-redux plugin
  • React Redux Getting Started Example
  • A brief discussion on the connection between React and Redux react-redux

<<:  The difference between mysql outer join and inner join query

>>:  Content-type description, that is, the type of HTTP request header

Recommend

HTML Web Page List Tags Learning Tutorial

HTML web page list tag learning tutorial. In HTML ...

Example code for implementing card waterfall layout with css3 column

This article introduces the sample code of CSS3 c...

Table paging function implemented by Vue2.0+ElementUI+PageHelper

Preface I have been working on some front-end pro...

How to change $ to # in Linux

In this system, the # sign represents the root us...

Implementation steps for docker-compose to deploy etcd cluster

Table of contents Write docker-compose.yml Run do...

Implementation of Nginx Intranet Standalone Reverse Proxy

Table of contents 1 Nginx Installation 2 Configur...

Solution to the problem that elements with negative z-index cannot be clicked

I was working on a pop-up ad recently. Since the d...

How to implement remote access control in Centos 7.4

1. SSH remote management SSH is a secure channel ...

Web Design Experience

<br />The author used to be a novice in web ...

Some tips for using less in Vue projects

Table of contents Preface 1. Style penetration 1....

Detailed explanation of how to enable slow query log in MySQL database

The database enables slow query logs Modify the c...