我正在为越来越大的项目使用样板。
我意识到我的大多数选择器/归约器代码都可以视为重复代码:
我的减速器设置正在执行的动作的状态,并在商店中的某个位置设置该动作的结果。您将在下面找到一个示例
...
case ACTION_NAMEpending:
return state
.setIn(['ACTION_NAME', 'pending'], true)
.setIn(['ACTION_NAME', 'fulfilled'], false)
.setIn(['ACTION_NAME', 'rejected'], false);
case ACTION_NAMEfulfilled:
return state
.set('AGGREGATE', action.AGGREGATEPATH)
.setIn(['ACTION_NAME', 'pending'], false)
.setIn(['ACTION_NAME', 'fulfilled'], true)
.setIn(['ACTION_NAME', 'rejected'], false);
...
我的选择器返回这些动作的状态值和动作的结果
export const makeSelectACTION_NAMEStatus = () =>
createSelector(selectREDUCState, state => ({
pending: state.getIn([
'ACTION_NAME',
'pending',
]),
fulfilled: state.getIn([
'ACTION_NAME',
'fulfilled',
]),
rejected: state.getIn([
'ACTION_NAME',
'rejected',
]),
error: state.getIn([
'ACTION_NAME',
'error',
]), }));
我最终将有许多这样的reduces-selector对,它们的作用完全相同。只有商店内部的路径发生了变化,而汇总发生了变化。
我想知道将它们归纳并从容器中传递变量是否是一个好习惯,以避免重复代码过多? 对于选择器,它给出:
`export const makeSelectActionStatus = (storePath) =>
createSelector(selectReducerState, reducerState => ({
pending: REDUCER_NAMEState.getIn([ ...storePath, 'pending' ]),
fulfilled: REDUCER_NAMEState.getIn([ ...storePath, 'fulfilled' ]),
rejected: REDUCER_NAMEState.getIn([ ...storePath, 'rejected' ]),
error: REDUCER_NAMEState.getIn([ ...storePath, 'rejected' ])
})); `
容器将使用类似的选择器
`
const mapStateToProps = createStructuredSelector({
someActionStatus: makeSelectActionStatus('MY_PATH'),
});
`
减速器将是类似的东西。 我的问题和担忧是让容器知道存储路径...这可以接受吗?