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);