当前位置:网站首页>一文带你了解redux的工作流程——action/reducer/store
一文带你了解redux的工作流程——action/reducer/store
2022-07-22 05:28:00 【蜡笔雏田学代码】
文章目录
今天来学习下react中另一个重要的知识:
redux
及其工作流程和案例分析
感兴趣的小伙伴一起来看看吧~
redux
redux理解
中文文档:https://www.redux.org.cn/
Github:https://github.com/reduxjs/redux
redux是什么
- redux是一个专门用于做
状态管理
的JS库(不是react插件库)。 - 它可以用在react,angular,vue等项目中,但基本与react配合使用。
- 作用:集中式管理react应用中多个组件
共享
的状态。
什么情况下需要使用redux
- 某个组件的状态,需要让其他组件可以
随时拿到
(共享)。 - 一个组件需要改变另一个组件的状态(通信)。
- 总体原则:能不用就不用,如果不用比较吃力才考虑使用。
redux工作流程
redux的三个核心概念
action
1️⃣ 动作的对象
2️⃣ 包含2个属性
- type:标识属性,值为字符串,唯一,必要属性
- data:数据属性,值类型任意,可选属性
3️⃣ 例子:{type:‘ADD_STUDENT’,data:{name:‘tom’,age:18} }
reducer
1️⃣ 用于初始化状态
,加工状态
。
2️⃣ 加工时,根据旧的state和action,产生新的state的纯函数
。
store
1️⃣ 将state、action、reducer联系在一起的对象
2️⃣ 如何得到此对象?
- import {createStore} from ‘redux’
- import reducer from ‘./reducers’
- const store = createStore(reducer)
3️⃣ 此对象的功能?
getState():
得到statedispatch(action):
分发action, 触发reducer调用, 产生新的statesubscribe(listener):
注册监听, 当产生了新的state时, 自动调用
redux的核心API
createstore()
作用:创建包含指定reducer的store对象
store对象
- 作用:redux库
最核心的管理对象
- 它内部维护着:
- state
- reducer
- 核心方法:
- getState()
- dispatch(action)
- subscribe(listener)
- 具体编码:
- store.getState()
- store.dispatch({type:‘INCREMENT’,number})
- store.subscribe(render)
applyMiddleware()
作用:应用上基于redux的中间件
(插件库)
combineReducers()
作用:合并多个reducer函数
使用redux编写应用
案例需求
求和案例:
有五个按钮,下拉按钮选择加数,点击加号或减号按钮,将当前求和数与下拉选择的数进行运算,得到当前的求和数,“当前求和为奇数”按钮表示当前求和为奇数时进行加法运算,“异步加”按钮表示等待0.5s再进行加法运算
效果:
️原生react版写法
Count组件 => index.jsx:
import React, {
Component } from 'react'
export default class Count extends Component {
state = {
count: 0
}
// 加法
increment = () => {
// 获取用户输入
const {
value } = this.selectNumber
// 读取原来的状态值
const {
count } = this.state
this.setState({
count: count + value * 1 })
}
// 减法
decrement = () => {
// 获取用户输入
const {
value } = this.selectNumber
// 读取原来的状态值
const {
count } = this.state
this.setState({
count: count - value * 1 })
}
// 奇数再加
incrementIfOdd = () => {
// 获取用户输入
const {
value } = this.selectNumber
// 读取原来的状态值
const {
count } = this.state
if (count % 2 !== 0) {
this.setState({
count: count + value * 1 })
}
}
// 异步加
incrementAsync = () => {
// 获取用户输入
const {
value } = this.selectNumber
// 读取原来的状态值
const {
count } = this.state
setTimeout(() => {
this.setState({
count: count + value * 1 })
}, 500)
}
render() {
return (
<div>
<h1>当前求和为:{
this.state.count}</h1>
<select ref={
c => this.selectNumber = c}>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button onClick={
this.increment}>+</button>
<button onClick={
this.decrement}>-</button>
<button onClick={
this.incrementIfOdd}>当前求和为奇数再加</button>
<button onClick={
this.incrementAsync}>异步加</button>
</div>
)
}
}
接下来的一篇文章,我会运用redux来实现这个案例的效果~
包含了redux精简版,完整版和异步action版的写法~
感兴趣的小伙伴浅浅期待下吧
原创不易,还希望各位大佬支持一下 \textcolor{blue}{原创不易,还希望各位大佬支持一下} 原创不易,还希望各位大佬支持一下
点赞,你的认可是我创作的动力! \textcolor{green}{点赞,你的认可是我创作的动力!} 点赞,你的认可是我创作的动力!
️ 收藏,你的青睐是我努力的方向! \textcolor{green}{收藏,你的青睐是我努力的方向!} 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富! \textcolor{green}{评论,你的意见是我进步的财富!} 评论,你的意见是我进步的财富!
边栏推荐
- Can0 transceiver + receive interrupt configuration and baud rate calculation of gd32f470 (detailed)
- SAP WPER(POS接口监控器)IDCO过账凭证ALV报表
- numpy.around
- JSON_ Incorrect problem returned by extract
- Digital path and practical thinking
- 接招吧。最强“高并发”系统设计 46 连问,分分钟秒杀一众面试者
- UE4 lifts the elevator by pressing the key
- [MySQL] SQL tuning practice teaching
- 汉得企业级数字化PaaS平台 HZERO 1.9.0 版本正式发布!
- [red team] att & CK - browser extension for persistence
猜你喜欢
Crack PLSQL by deleting the registry
5分钟带你浅谈企业级PaaS平台HZERO!
JSON_ Incorrect problem returned by extract
lvs看这篇就够了
UE4 modify the default cache path
[vs] how to check where the thread is blocked
Codeforce d2. RGB substring (hard version) sliding window
Under fitting and over fitting (regularization)
UE4 use of vegetation tools
Concis component library | dark pattern design
随机推荐
tf.compat
npm私服发包及使用
FPGA - 7系列 FPGA内部结构之Memory Resources -02- FIFO资源
UE4 set night (update skysphere according to directionallight direction)
[vs] how to check where the thread is blocked
codeforce D2. RGB Substring (hard version) 滑动窗口
Application of tensorflow optimizers in iris classification task
5 minutes to talk about the enterprise PAAS platform hzero!
开源多云应用敏捷全链路技术平台Choerodon猪齿鱼发布0.23版本
Qt5.9.2 initial import using msvc2017_ 64 record of problems encountered by compiler
SAP WPER(POS接口监控器)IDCO过账凭证ALV报表
禅道管理员忘记密码找回密码
UE4 enters the designated area to realize the trigger acceleration function
tf.placeholder
【Redis】分布式场景下Redis高可用部署方案
pytorch优化器: optim.SGD && optimizer.zero_grad()
Upgrade MySQL 5.6 to 8.0 on Windows
1840. 最高建筑高度 贪心
Are you still writing code for adding, deleting, modifying and checking? Direct one click generation
UE4 keyboard keys realize door opening and closing