当前位置:网站首页>Redux使用
Redux使用
2022-07-07 23:44:00 【是张鱼小丸子鸭】
目录
1,redux的工作流程
2.安装redux
npm i redux --save3.redux概念了解
redux是一个专门用于做状态管理的js库(不是react插件库)
可以用在react,angular,vue项目中,基本与react配合使用
作用:集中式管理react应用中多个组件共享的状态
4.创建Redux中的仓库-store和reducer
首先,创建一个store文件夹,然后创建一个index.js文件,再创建一个reducer.js文件
import { createStore } from "redux";
//引入reducer文件
import reducer from './reducer'
let store=createStore(reducer)
export default storereducer.js文件
const defaultState={
value:‘张三’,
list:[
'您好',
'hello'
]
}
//业务逻辑可以写这里
export default (state=defaultState,action)=>{
return state
}这样,简单的store和reducer就搭建好了,现在我们在组件中使用store仓库
首先进行引入
import store from './store'
然后我们可以打印一下store,打印结果如下,他有一个getState()方法

然后在页面渲染我们getState里面的值即可
注:reducer只能接收state,不能改变state
5.redux中间件( Redux-thunk)

安装
npm i redux-thunk --save
配置
import { createStore,applyMiddleware } from "redux";
import thunk from 'redux-thunk'
//引入reducer文件
import reducer from './reducer'
let store=createStore(reducer,applyMiddleware(thunk))
export default storecreatStore只能接收两个参数,如果想要定义多个参数,我们可以使用增强函数(compose)
异步处理写在中间件Redux-thunk中
解决异步action不能使用function方法,如果我们不使用中间件,那么我们如果在action中使用函数,那么他会进行报错
6.combineReducers使用
作用:合并多个reducer函数
redux的combineReducers()可以方便的将redux的reducer拆分为各个小的reducer文件,统一调用各自的reducer,方便统一管理
creatStore只能接收两个参数,如果想要定义多个参数,我们可以对多个reduce进行合并,使用combineReducers
import { createStore,applyMiddleware,combineReducers } from "redux";
import countReduce from '../reduces/count_reduce'
import todoReduce from '../reduces/todolist_reduce'
import thunk from 'redux-thunk'
// 使用redux官方提供的合并reduce方法
const allReducer = combineReducers({
count:countReduce,
todolist:todoReduce
})
// 第一个参数为全部reduce,第二个参数为使用支持异步action的中间件
let store = createStore(allReducer,applyMiddleware(thunk))
export default store边栏推荐
- 2021 Shanghai safety officer C certificate examination registration and analysis of Shanghai safety officer C certificate search
- LeetCode 练习——剑指 Offer 36. 二叉搜索树与双向链表
- Anaconda3 download address Tsinghua University open source software mirror station
- 不算不知道,花呗分期的真实利率居然这么高
- Matlab code about cosine similarity
- How to get the first and last days of a given month
- 滑环在直驱电机转子的应用领域
- 2021-03-06 - play with the application of reflection in the framework
- Usage of xcolor color in latex
- 2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
猜你喜欢

COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model

碳刷滑环在发电机中的作用
![[loss function] entropy / relative entropy / cross entropy](/img/bc/574a4745336b0baf1a4ca53af41a82.jpg)
[loss function] entropy / relative entropy / cross entropy

子矩阵的和

Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design

General configuration toolbox

Content of one frame

Use "recombined netlist" to automatically activate eco "APR netlist"

4. Apprentissage stratégique

Different methods for setting headers of different pages in word (the same for footer and page number)
随机推荐
不算不知道,花呗分期的真实利率居然这么高
2022 high voltage electrician examination skills and high voltage electrician reexamination examination
Anaconda3 tutorial on installing and adding Tsinghua image files
Application of state mode in JSF source code
Overall introduction of the project
滑环在直驱电机转子的应用领域
QT -- package the program -- don't install qt- you can run it directly
COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
小金额炒股,在手机上开户安全吗?
Write a pure handwritten QT Hello World
Redis master-slave replication
The examination contents of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge) in 2021 and the free examination questions of the third batch of Guangdong Prov
[loss function] entropy / relative entropy / cross entropy
regular expression
Chapter improvement of clock -- multi-purpose signal modulation generation system based on ambient optical signal detection and custom signal rules
QT--创建QT程序
Kafka connect synchronizes Kafka data to MySQL
The Ministry of housing and urban rural development officially issued the technical standard for urban information model (CIM) basic platform, which will be implemented from June 1
QT build with built-in application framework -- Hello World -- use min GW 32bit
2022 safety officer-c certificate examination paper and safety officer-c certificate simulated examination question bank