Redux知识点

Redux

1、redux是什么?
  • 是一个框架
  • 跨组件通信
分四块内容
  1. actionTypes
  2. actions => {type, payload}
  3. reducer
  4. store
redux三大原则
  1. 单一数据源
  2. State 是只读的
  3. 使用纯函数来执行修改
2、provider
  • 只有一个属性,是store
  • 顶层容器
3、connect
  • 伪高阶函数
  • 4个参数
1、把redux中state映射成的props传递到组件中去   // 获取state数据
const mapStatetoProps = (state,ownProps)=>{
    // state  redux存储的数据
    // ownProps   组件调用时传过来的props
    // 返回一个对象
}

2、把dispatch操作封装在当前函数里,传递到组件中   // 封装方法
const mapDispatchtoProps = (dispatch, ownProps)=>{
    // 返回一个对象
    return {
        fetchList:()=>{
            axios.get('路径')
            .then(res=>{
                // 触发dispatch
                dispatch({
                    type:'FETCH_LIST',
                    payload:res.data
                })
            })
        }
    }
    
}

3、合并props
const mergeProps = (stateProps, dispatchProps, ownProps)=>{
    // stateProps 是mapStateToProps的返回值
    // dispatchProps 是mapDispatchtoProps的返回值
    // ownProps 是调用时传入的props
    return {...stateProps, ...dispatchProps, ...ownProps}
    
}

4、可选配置项
const options = {
    pure:true
    // pure:表示state需要深拷贝才会响应
    // true 深拷贝(默认true 深拷贝)
    // false 浅拷贝深拷贝都可以
}
4、reducer
  • 有两个参数:state action
  • 接收先前的 state 和 action,并返回新的 state
5、深拷贝和浅拷贝
  • 深拷贝 …
  • 浅拷贝 Object.assign()
5、store
  • createStore接收两个参数

reducers 存储数据和修改数据

applyMiddleware 应用的中间件

6、combineReducers // 合并
import {combineReducers} from 'redux';
import cart from './cart';
import my from './my';

export default combineReducers({
    cart,
    my
})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值