const ACTION_HANDLERS = {
[CONSTANTS.REPORT_CATEGORY_LIST]:(state, action) => {
const reportCategories = action.payload;
//让分类列表为空,需要传递一个空数组.
if(!!reportCategories){
return Object.assign({},state, {reportCategories:action.payload});
}
}
}
const initialState = {
reportCategories:[],
openedReportCategoryId:null,
};
export default function appReducer(state = initialState, action) {
const handler = ACTION_HANDLERS[action.type];
return handler ? handler(state, action) : state;
}
这是
ES6 Object literals
的一个叫做Computed property keys
的新特性。一般来说定义一个Object时,属性的名字是确定的,而这里是个变量,也就是是在对象生成的时候才能得出的。在你的例子中假如
CONSTANTS.REPORT_CATEGORY_LIST = 'report'
, 那么这个ACTION_HANDLERS
就相当于:使用这种
[]
, 就上面的例子还有一种写法是: