Redux

1,安装yarn add redux后再安装 yarn add react-redux
2,connect (mapStateToProps,mapDispatchToProps)(组件名)
mapStateToProps指的是connect组件和store做链接时,如何将store的数据映射到Props上面,
mapDispatchToProps中的dispatch是所有的方法合集

3,当reducer传入的数据太多时候得将数据分流到子页面中,这时候会用到 combineReducers

4,actionCreators里边传入的是函数: 是type的判断类型

export const  searchFocus = () => ({
    type:'search_focus'
});

export const  searchBlur = () => ({
    type:'search_blur'
});

5,constants 常量的 写入 例如:
export const SEARCH_FOCUS = 'header/search_focus'; export const SEARCH_BLUR ='header/search_blur'

6,immutable 防止state数据的篡改

安装 yarn add immutable

import  {fromJS} from 'immutable'  //
const defaultState = fromJS({
    focused:false
});

export default (state = defaultState,action) =>{
    if(action.type===constants.SEARCH_FOCUS) {
        return state.set('focused',true)
    }
    if(action.type===constants.SEARCH_BLUR) {
        return state.set('focused',false)
    }
    return  state;
}

7,redux-immutable 统一数据格式
原因:使用immutablestate后此时的state是js对象,而state.header是immutable对象为统一则使用 redux-immutable
安装:yarn add redux-immutable

const mapStateToProps =(state)=> {//此处的state指的是store的state,会将state传递给Props
    return{
        focused: state.header.get('focused')  //此处用get是因为此时的state.headerimmutable对象,得遵循immutable的写法
    }
}

此时的代码可改为 :focused: state.get('header').get('focused')
或者:focused: state.getin(['header','focused'])

且此时reducer中改为import {combineReducers} from 'redux-immutable'; 这样conbineReducers中的 header数据为 immutable对象格式

8,registerServiceWorker 具体参考(https://www.cnblogs.com/wphl-27/p/10393666.html)
在生产环境中可用可不用

9,ApplyMiddleware
此方法由redux提供

10,redux-thunk

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值