react reducer的拆分 整合 combineReducers

  1. 在组件文件夹下创建一个文件夹store
  2. 下面创建也个reducer.js 
    1. const defaultState = {
      }
      export default (state = defaultState,action) => {
          return state;
      }

       

  3. 项目下的store文件下的reducer.js用combineReducers整合

  4.  

    import { combineReducers } from 'redux'
    import { reducer as headerReducer} from '../common/header/store';
    
    const reducer = combineReducers({
        header:headerReducer
    })
    
    export default reducer;

     

  5. 上面报错--再组件中的strore中创建index.js --

  6. import reducer from './reducer';
    
    export { reducer };

    这样可以优化路径

  7. 优化action

    1. header文件夹下的store文件夹下创建actionCreators.js

    2. import * as actionTypes from './actionTypes'
      
      export const searchFocus = () => ({
          type: actionTypes.SEARCH_FOCUS
      });
      export const searchBlur = () => ({
          type: actionTypes.SEARCH_BLUR
      }) 

       

    3. header文件夹下的store文件夹下创建actionTypes.js

    4. export const SEARCH_FOCUS = 'header/SEARCH_FOCUS';
      export const SEARCH_BLUR = 'header/SEARCH_BLUR';

       

    5. 把header文件夹下的store文件夹下的所有文件都在index.js中导出

    6. import reducer from './reducer';
      import * as actionCreators from './actionCreators';
      import * as actionTypes  from './actionTypes';
      
      export { reducer , actionCreators , actionTypes};

       

    7. 在header文件夹下的index.js引用actionCreators 

    8. import { actionCreators } from './store';

      代码改变

      return {
      		handleInputFocus () {
      
      			dispatch(actionCreators.searchFocus())
      		},
      		handleInputBlur(){
      			dispatch(actionCreators.searchBlur())
      		}
      	}

       

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值