当前位置:网站首页>Redux usage
Redux usage
2022-07-08 01:38:00 【It's a fish, a small meatball, a duck】
Catalog
3.redux Conceptual understanding
4. establish Redux Warehouse in -store and reducer
5.redux middleware ( Redux-thunk)
1,redux workflow
2. install redux
npm i redux --save3.redux Conceptual understanding
redux It is a special tool for state management js library ( No react Plug-in library )
Can be used in react,angular,vue In the project , Basic and react In combination with
effect : Centralized management react The state shared by multiple components in the application
4. establish Redux Warehouse in -store and reducer
First , Create a store Folder , And then create a index.js file , Create another reducer.js file
import { createStore } from "redux";
// introduce reducer file
import reducer from './reducer'
let store=createStore(reducer)
export default storereducer.js file
const defaultState={
value:‘ Zhang San ’,
list:[
' Hello! ',
'hello'
]
}
// Business logic can be written here
export default (state=defaultState,action)=>{
return state
}such , ordinary store and reducer Just set it up , Now we use store Warehouse
First, introduce
import store from './store'
Then we can print it store, The results are as follows , He has one getState() Method

Then render us on the page getState Just the value inside
notes :reducer receive calls only state, Can't change state
5.redux middleware ( Redux-thunk)

install
npm i redux-thunk --save
To configure
import { createStore,applyMiddleware } from "redux";
import thunk from 'redux-thunk'
// introduce reducer file
import reducer from './reducer'
let store=createStore(reducer,applyMiddleware(thunk))
export default storecreatStore Only two parameters can be received , If you want to define multiple parameters , We can use enhancement functions (compose)
Asynchronous processing is written in middleware Redux-thunk in
Resolve asynchrony action Out of commission function Method , If we don't use middleware , So if we are action Using functions in , Then he will report an error
6.combineReducers Use
effect : Merge multiple reducer function
redux Of combineReducers() It's convenient to put redux Of reducer Split into small reducer file , Call their own reducer, Convenient unified management
creatStore Only two parameters can be received , If you want to define multiple parameters , We can deal with many reduce A merger , Use combineReducers
import { createStore,applyMiddleware,combineReducers } from "redux";
import countReduce from '../reduces/count_reduce'
import todoReduce from '../reduces/todolist_reduce'
import thunk from 'redux-thunk'
// Use redux Officially provided merger reduce Method
const allReducer = combineReducers({
count:countReduce,
todolist:todoReduce
})
// The first parameter is all reduce, The second parameter is to support asynchronous action Middleware
let store = createStore(allReducer,applyMiddleware(thunk))
export default store边栏推荐
- 解决报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
- 云原生应用开发之 gRPC 入门
- NPM internal split module
- 2021 welder (primary) examination skills and welder (primary) operation examination question bank
- LeetCode 练习——剑指 Offer 36. 二叉搜索树与双向链表
- Introduction to natural language processing (NLP) based on transformers
- 2021-03-14 - play with generics
- Redis master-slave replication
- 2022 chemical automation control instrument examination summary and chemical automation control instrument simulation examination questions
- Kafka connect synchronizes Kafka data to MySQL
猜你喜欢

快速熟知XML解析

ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!

Led serial communication

城市土地利用分布数据/城市功能区划分布数据/城市poi感兴趣点/植被类型分布

Talk about smart Park

解决报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

Anaconda3 download address Tsinghua University open source software mirror station

从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值

Guojingxin center "APEC education +" Shanghai Jiaotong University Japan Cooperation Center x Fudan philosophy class "Zhe Yi" 2022 New Year greetings

The beauty of Mathematics -- the principle of fine Fourier transform
随机推荐
MATLAB R2021b 安装libsvm
2022 operation certificate examination for main principals of hazardous chemical business units and main principals of hazardous chemical business units
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
Euler Lagrange equation
5. Discrete control and continuous control
How does Matplotlib and PIL image integrate and save multiple pictures into one picture
regular expression
LaTeX 中 xcolor 颜色的用法
Guojingxin center "APEC education +" Shanghai Jiaotong University Japan Cooperation Center x Fudan philosophy class "Zhe Yi" 2022 New Year greetings
Smart agricultural technology framework
Understanding of maximum likelihood estimation
项目经理有必要考NPDP吗?我告诉你答案
[loss function] entropy / relative entropy / cross entropy
Frequency probability and Bayesian probability
写一个纯手写的qt的hello world
qt-使用自带的应用框架建立--hello world--使用min GW 32bit
NPM internal split module
2022 safety officer-a certificate free examination questions and safety officer-a certificate mock examination
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
Kafka connect synchronizes Kafka data to MySQL