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

A brief discussion on MySQL select optimization solution

Table of contents Examples from real life Slow qu...

Ubuntu Basic Tutorial: apt-get Command

Preface The apt-get command is a package manageme...

How InnoDB cleverly implements transaction isolation levels

Preface In the previous article Detailed Explanat...

Example code for implementing complex table headers in html table

Use HTML to create complex tables. Complex tables...

Docker link realizes container interconnection

Table of contents 1.1. Network access between con...

About Vue's 4 auxiliary functions of Vuex

Table of contents 1. Auxiliary functions 2. Examp...

Solution to BT Baota Panel php7.3 and php7.4 not supporting ZipArchive

The solution to the problem that the PHP7.3 versi...

How to create a trigger in MySQL

This article example shares the specific code for...

Analysis of the advantages and disadvantages of MySQL stored procedures

MySQL version 5.0 began to support stored procedu...

A brief talk on responsive design

1. What is responsive design? Responsive design i...

Solution to MySql service disappearance for unknown reasons

Solution to MySql service disappearance for unkno...

js implements a simple countdown

This article example shares the specific code of ...

Detailed explanation of the usage of scoped slots in Vue.js slots

Table of contents No slots Vue2.x Slots With slot...

Several ways to easily traverse object properties in JS

Table of contents 1. Self-enumerable properties 2...

About the problem of offline installation of Docker package on CentOS 8.4

The virtual machine used is CentOS 8.4, which sim...