数据都放在reducer.js下不利于对数据进行管理,可以把一个大的reducer.js拆分成多个小的reducer.js。
小的reducer.js
const defaultState={ focused:false } export default(state=defaultState,action)=>{ if(action.type=='input_focus'){ return {focused:true} } if(action.type=='input_blur'){ return {focused:false} } return state; }
大的reducer.js
import {combineReducers} from 'redux' import {reducer as headerReducer} from '../common/header/store' const reducer=combineReducers({ header:headerReducer }); export default reducer;
注意在组件中使用“state.header.focused”而不是“state.focused”
const mapStateToProps = (state) => { return { focused:state.header.focused } }