redux简单学习[ store, action, reducer ]redux
combineReducers,合并多个reducersegmentfault
若有下面两个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,则为code
// reducers/index.js import { combineReducers } from 'redux'; import textApp from './textApp'; import todoApp from './todoApp'; export default combineReducers({ textApp, todoApp });
则调用时能够写成这样orm
import reducer from './reducers' let store = createStore(reducer);