js手札--redu简单学习(二)

redux简单学习(二)

redux简单学习[ store, action, reducer ]

1. combineReducers

combineReducers,合并多个reducer

如有下面两个reducer,todoApp,textApp

// reducers/todoApp.js

export default function todoApp(state, action) {
  switch (action.type) {
    case 'add':
      return Object.assign({}, state, {
          result : action.a + action.b
      })
    case 'sub':
      return Object.assign({}, state, {
          result : action.a - action.b
      })
    default:
      return state
  }
}
// reducers/textApp.js

export default function todoApp(state, action) {
  switch (action.type) {
    case 'normal':
      return Object.assign({}, state, {
          result : action.text
      })
    case 'camel':
      return Object.assign({}, state, {
          result : action.text.replace(/-[^-]{1}/g, (m) => m[1].toUpperCase())
      })
    default:
      return state
  }
}

换成combineReducers,则为

// reducers/index.js

import { combineReducers } from 'redux';
import textApp from './textApp';
import todoApp from './todoApp';

export default combineReducers({
  textApp,
  todoApp
});

则调用时可以写成这样

import reducer from './reducers'

let store = createStore(reducer);

关键字:JavaScript, action, state, todoapp

版权声明

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部