它说
多做行动创造者,少做减速器。
所以,我应该:
this.props.fileActions.addFile( Object.keys(this.props.fileById).length )
export const addFile(){ return (dispatch, getState) =>{ const filelength = Object.keys(getState().fileById).length; dispatch(addFileAction(filelength)) } }
我来自棱角世界,在大多数情况下,我将数据存储在服务中,调用Service.addFile(),而files.length已经存储在服务中,因此不需要传递Service.addFile(files.length)
发布于 2016-11-22 02:49:02
更喜欢后一种用法。
从组件中将params传递给操作创建者将使您的组件与数据耦合,这意味着您可能无法重用该组件。
发布于 2016-11-22 10:54:09
对于用例,似乎应该在还原器中这样做。
function addNewFile() {
return {
type: 'ADD_NEW_FILE'
}
}
function filesReducer(state, action) {
if(action.type === 'ADD_NEW_FILE') {
const newId = state.filesById + 1;
return {
filesById: state.filesById.concat(newId),
entities: Object.assign({}, state.entities, {
[newId]: {name: `File ${newId}`}
})
};
}
// some other action handlers
return state;
}
您将在某些操作中修改状态。这正是减速机应该做的。
https://stackoverflow.com/questions/40716983
复制相似问题