三、【react-redux】数据共享
创始人
2024-03-06 05:01:07
0

文章目录

  • 1、优化项目结构
  • 2、添加一个新容器组件
    • 2.1、新项目结构
    • 2.2、CODE
      • 2.2.1、reduc/constant.js
      • 2.2.2、redux/actions/person.js
      • 2.2.3、redux/reducers/person.js
      • 2.2.4、redux/store.js
      • 2.2.5、Count.jsx
      • 2.2.6、Person.jsx
    • 2.3、Result
  • 3、总结

本示例修改自 上一章 求和Demo

1、优化项目结构

  • 将redux下的action和reducer单独分类方便管理,因为现在要写第二个第三个第n个容器组件了

在这里插入图片描述

2、添加一个新容器组件

2.1、新项目结构

在这里插入图片描述

2.2、CODE

2.2.1、reduc/constant.js

/*** action.type常量*/
// for Count
export const INCREMENT = 'increment'
export const DECREMENT = 'decrement'
// for Person
export const ADD_PERSON = 'add_person'

2.2.2、redux/actions/person.js

import { ADD_PERSON } from '../constant'// 创建添加一个人信息的动作对象
export const createAddPersonAction = data => ({ type: ADD_PERSON, data })

2.2.3、redux/reducers/person.js

import { ADD_PERSON } from '../constant'export default function personReducer(preState = [], action) {const { type, data } = actionswitch (type) {case ADD_PERSON:return [data, ...preState]default:return preState}
}

2.2.4、redux/store.js

import {// 引入createStore,专门用于创建redux中最为核心的store对象createStore,// 引入applyMiddleware 执行中间件applyMiddleware,// 引入 combineReducers 组合 reducerscombineReducers
} from 'redux'
//引入reducers
import countReducer from './reducers/count'
import personReducer from './reducers/person'
// 引入 redux-thunk 用于支持异步action
import thunk from 'redux-thunk'// 共享就得把 reducer 组合到一起传进去
const allReducers = combineReducers({count: countReducer,people: personReducer
})//暴露store
export default createStore(allReducers, applyMiddleware(thunk))

2.2.5、Count.jsx

// import 未改变,略...class Count extends Component {// 方法等都未改变,略...render() {const { count, people } = this.propsreturn (

我是Count,下面已经添加了{people.length}个人

{/* 内容全部未变,略... */}
)} }// 暴露容器组件 export default connect(state => ({count: state.count,people: state.people}),{// 未改变,略...} )(Count)

2.2.6、Person.jsx

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { nanoid } from 'nanoid'
import { createAddPersonAction } from '../../redux/actions/person'class Person extends Component {state = {name: '',age: '',}submit = () => {this.props.add({id: nanoid(),...this.state})// 清空表单this.setState({name: '',age: ''})}render() {const { name, age } = this.stateconst { people, count } = this.propsreturn (

我是Person,上面求和为{count}

name}onChange={e => this.setState({ name: e.target.value })}/>
age}onChange={e => this.setState({ age: e.target.value })}/>

    {people.map(i => (
  • i.id}>{`姓名:${i.name}-年龄:${i.age}`}
  • ))}
)} }export default connect(state => ({people: state.people,count: state.count}),{add: createAddPersonAction} )(Person)

2.3、Result

在这里插入图片描述

3、总结

  1. 定义一个Pserson组件,和Count组件通过redux共享数据
  2. 为Person组件编写:reducer、action,配置constant常量
  3. 重点:Person的reducer和Count的Reducer要使用combineReducers进行合并,合并后的总状态是一个对象!!!
  4. 交给store的是总reducer,最后注意在组件中取出状态的时候,记得“取到位”

*补充:纯函数

  1. 一类特别的函数: 只要是同样的输入(实参),必定得到同样的输出(返回)
  2. 必须遵守以下一些约束
  3. 不得改写参数数据
  4. 不会产生任何副作用,例如网络请求,输入和输出设备
  5. 不能调用Date.now()或者Math.random()等不纯的方法

redux的reducer函数必须是一个纯函数

相关内容

热门资讯

*ST沐邦控股股东陷借款纠纷 ... 12月24日,*ST沐邦(603398)发布公告,控股股东沐邦新能源控股所持部分股份可能被司法拍卖。...
萧山网友收到一条短信:该政策取... 昨天,萧山一网友在萧内网App发帖: 这下好了,以后从萧山去诸暨,走高速没有免费了! 据了解,...
社保年限再降低!一图看懂北京住... 新京报贝壳财经记者 段文平 制图 任婉晴 编辑 杨娟娟 校对 柳宝庆
原创 猪... 广东潮州一打假博主举报展会摊贩卖假牛肉(实为猪肉),竟在市场监管局工作人员眼皮底下,遭六七名暴徒围殴...
供应精矿后被拖欠超2亿元货款 ... 12月24日晚间,普路通(SZ002769)公告称,子公司广州市普路通供应链管理有限公司(以下简称“...
遭冒用名义散布不实招生政策,清... 24日,清华大学招生办公室通过微信公众号发表声明如下: 近日,我办接到多所中学、家长及学生反映,有...
国家矿山安全监察局公布矿山智能... 人民财讯12月24日电,国家矿山安全监察局12月24日公布矿山智能化建设相关激励政策,其中提出,在煤...
中航基金杨中楷:政策+技术+资... 近日,A股商业航天板块在资本市场热度骤升。中航基金总监助理杨中楷在接受上海证券报记者专访时深入解读商...
北京:优化政策放宽非京籍家庭购... 新华社北京12月24日电(记者郭宇靖)为贯彻落实中央经济工作会议精神,着力稳定房地产市场,12月24...