reduxreact-redux

news/2025/2/1 20:43:15/

redux

redux组成部分:state,action,reducer,store
store主要职责:
维持应用的state
提供getState()方法获取state
提供dispatch()方法发送action
通过subscribe()来注册监听
通过subscribe()返回值来注销监听
用法:

  • action:必须要有return返回值,返回的是对象{type: ‘xx’, key: value},必须要有type属性
action/index.js组件写action的相关信息
const sendAction = () => { // 名字随意取return {type: 'send_action', // 名字随意取...//剩下的写需要传的参数value: '发送爱心~' // 调用时返回的值}
}
const stopAction = () => { // 名字随意取return {type: 'stop_action', // 名字随意取...//剩下的写需要传的参数value: '停止发送'}
}
module.exports = {sendAction,stopAction,
}
  • reducer:可以写个初始值,在这边判断action的type值
reducer/index.js组件
const initState = {sendValue: '发射信息',stopValue: '停止信息',
} // 定义初始值const reducer = (state = initState, action) {switch(action.type) {case: 'send_action':return {sendValue: action.Value}case: 'stop_action':return {stopValue: action.Value}case: 'add_action':retrun {// 返回的都是新的state,那我随便取个值吧addValue: action.value}default: return state;}
}const loveReducer = (state = initState, action) {switch() {...}}
const ... = (state, action) {}
module.exports = {reducer,loveReducer,...
}
  • store: 需要导入reducer
import { legacy_createStore as createStore } from 'redux';// 导入reducer
import { reducer, xxx } from '../reducer/index.js';// 构建store
export default createStore(reducer)
  • 使用:在需要发送action的页面导入action,store
import React from 'react';
import store from '../../store/index.js';
import { sendAction, stopAction } from '../../action/index.js';export default class Home extends React.Component {handleClick = () => {// 发送action方式有两种:1.发送action组件内的const action = sendAction()store.dispatch(action)// 2.直接store.dispatchstore.dispath({type: 'add_action', // action的type必传// 剩下的可以传值value: 'add_action的传值,这个值会在reducer的action里'})}// 当组件加载完成的时候监听,具体用法还需百度componentDidMount(){store.subscribe(() => {// this.setState({});})}render(){return (<><button onClick="this.handleClick">点击发送action</button></>)}
}

child组件中使用add_action的value值
获取store里的属性值用store.getState()

import React from 'react';
// 需要引入store
import store from '../../store/index.js';
export default class Child extends React.Commponents {render(){return (<div>{ store.getState().addValue }</div>)}
}

react-Redux

react-Redux中两个重要的成员:
1.Provider: 这个组件能够使你整个app都能获取到store中的数据
2.connect: 这个方法能够使组件跟store来进行关联

  • Provider:
    Provider包裹在根组件最外层,使所有的子组件都可以拿到State
    Provider接收store作为props,然后通过context往下传递,这样react中任何组件都可以通过context获取到store

  • connect:
    connect:Provider内部组件如果想要使用到state中的数据,就必须要connect进行一层包裹封装,换一句话来说就是必须要被connect进行加强
    connect就是方便我们组件能够获取到store中的state

react-redux使用:需要结合redux的reducer,store使用

  1. 需要在引入两个子组件的根组件最外层,引入store,绑定store
import React from 'react';
import srore from '../../store/index.js'; // 与上面store/index.js相同
import { Provider } from 'react-redux';
// 引入两个子组件
import ComA from '../xxx/ComA.js';
import ComB from '../xxx/ComB.js';
class Main extends React.Commpont {render(){return(<Provider store={store}><ComA /><ComB /></Provider>)}
}
  1. 在两个子组件中使用connect,一个发送组件一个接收组件
    connect(接收函数,发送函数)(需要使用connect的组件)
ComA组件:发送action组件
import React from 'react';
// 导入connect
import { connect } from 'react-redux';
class ComA extends React.Commponent{addClick = () => {// 底下mapDispatchToProps 函数在组件内就是props,用this.props.xxx可以调用具体的actionthis.props.addAction();}recClick = () => {this.props.recAction();}render() {refturn {<><button onClick={this.addClick}>+</button><button onClick={this.recClick}>-</button></>}}
}const mapDispatchToProps = dispatch => {// 接收dispatch参数// 必须要有return 返回值,返回对象{}, 键值对形式 key: ()=>回调函数return {addAction: () => {dispath({type: 'add_action', // action必须要有type值value: '需要传递的值',})},recAction: () => {dispath({type: 'rec_action', // action必须要有type值value: '需要传递的值',})}}
}
// connect需要导出,第一个函数是接收的,这个组件不需要接收置为null,只需要第二个发送函数,名字随便取
export default connect(null, mapDispatchToProps)(ComA)
reducer/index.js组件
const initState = {count: 0
}
exports.reducer = (state, action) => {switch(action.type){case: 'add_action':return {count: state.count + 1}case: 'rec_action':return {count: state.count - 1}}
}

child组件中接收value:

import React from 'react';// 导入connect
import { connect } from 'react-redux';class ComB extends React.Commponent {render() {return(// 在组件内使用this.props获取值<div>{ this.props.count }</div>)}
}const mapStateToProps = (state) => {// 必须要returnreturn state
}
// 接收组件只需要第一个接收函数,需要导出
export default connect(mapStateToProps)(ComB)

http://www.ppmy.cn/news/1085241.html

相关文章

大数据精准营销怎么满足用户的个性化需求?

近年来在AI和媒体的带动下&#xff0c;大数据分析不断介入&#xff0c;各行各业都开始陆续依仗大数据营销这棵大树&#xff0c;以此来更加高效、便捷、智能、精准的服务于用户。 这就像追求恋人一样&#xff0c;投其所好方能成为眷属。 大数据精准营销的好处&#xff1a; 相…

Day53|leetcode 1143.最长公共子序列、1035.不相交的线、53. 最大子序和

leetcode 1143.最长公共子序列 题目链接&#xff1a;1143. 最长公共子序列 - 力扣&#xff08;LeetCode&#xff09; 视频链接&#xff1a;动态规划子序列问题经典题目 | LeetCode&#xff1a;1143.最长公共子序列_哔哩哔哩_bilibili 题目概述 给定两个字符串 text1 和 text2&…

c++自带的查找函数

一、binary_search 使用binary_search查找必须是排好序的才行。使用下面三个函数都需要先排一遍序。 //这三个函数都有三个参数&#xff1a;分别为数组的起始位置、数组的终止位置&#xff08;取不到&#xff09;以及要查找的目标值&#xff0c; lower_bound()&#xff1a;返回…

Web3 solidity编写cancelorder取消订单函数 并梳理讲述逻辑

上文 Web3 solidity订单池操作 中 我们讲述了订单池的基本概念 并手动编写了创建订单的操作 最近的 我们还是先将 ganache 环境起起来 然后 我们打开项目 上文中 我们写了makeOrder创建订单的函数 但是 也带出一个问题 我们创建之后 如果不要了 怎么干掉呀&#xff1f; js中我…

log日志

日志 1.1 作用&#xff1a; ​ 跟输出语句一样&#xff0c;可以把程序在运行过程中的详细信息都打印在控制台上。 ​ 利用log日志还可以把这些详细信息保存到文件和数据库中。 1.2 使用步骤&#xff1a; ​ 不是java的&#xff0c;也不是自己写的&#xff0c;是第三方提供…

【Linux】JumpServer 堡垒机远程访问

文章目录 前言1. 安装Jump server2. 本地访问jump server3. 安装 cpolar内网穿透软件4. 配置Jump server公网访问地址5. 公网远程访问Jump server6. 固定Jump server公网地址 前言 JumpServer 是广受欢迎的开源堡垒机&#xff0c;是符合 4A 规范的专业运维安全审计系统。JumpS…

Linux安装MySQL5.7.26教程图解

0、准备工作 下载MySQL软件包 ①、官网下载&#xff1a;https://www.cnblogs.com/linu-x/p/15701479.html#_label6 ②、百度网盘下载&#xff1a;百度网盘 请输入提取码 提取码&#xff1a;chao ③、文件说明 主机名 CentOS版本 MySQL版本 IP地址 test CentOS Linux …

MySQL的内置函数复合查询内外连接

文章目录 内置函数时间函数字符串函数数学函数其他函数 复合查询多表笛卡尔积自连接在where中使用子查询多列子查询在from中使用子查询 内连接外连接左外连接右外连接 内置函数 时间函数 函数描述current_date()当前日期current_time()当前时间current_timestamp()当前时间戳…