是否可以联合Reducer的ActionCreators?
Is it possible to unite Reducer's ActionCreators?
我想加入一些做完全相同事情的“on()
”,从而节省代码行,这可能吗?
给出我的代码示例:
const peopleReducer = createReducer(
INIT_STATE,
on(fromPeopleAction.GET_ALL_SUCCESS, (state, { peoples }) => adapter.addMany(peoples, { ...state, loading: false })),
on(fromPeopleAction.GET_ALL_FAIL, (state, { error }) => ({ ...state, error, loading: false })),
on(fromPeopleAction.CREATE_SUCCESS, (state, { people }) => adapter.addOne(people, { ...state, loading: false })),
on(fromPeopleAction.CREATE_FAIL, (state, { error }) => ({ ...state, error, loading: false })),
on(fromPeopleAction.DELETE_SUCCESS, (state, { id }) => adapter.removeOne(id, { ...state, loading: false })),
on(fromPeopleAction.DELETE_FAIL, (state, { error }) => ({ ...state, error, loading: false }))
);
请注意,我们有一些“on()
”做同样的事情,是否可以为同一个 on()
调用多个操作?
根据 on
's overloads 判断,如果您这样做,我认为您可以对多个操作使用相同的逻辑:
on(action1, action2, action3, ...action10, (state, action) => {})
请注意,在 10 actions 之后,您将丢失类型。
同样重要的是要注意最后一个参数应该是reducer。
我想加入一些做完全相同事情的“on()
”,从而节省代码行,这可能吗?
给出我的代码示例:
const peopleReducer = createReducer(
INIT_STATE,
on(fromPeopleAction.GET_ALL_SUCCESS, (state, { peoples }) => adapter.addMany(peoples, { ...state, loading: false })),
on(fromPeopleAction.GET_ALL_FAIL, (state, { error }) => ({ ...state, error, loading: false })),
on(fromPeopleAction.CREATE_SUCCESS, (state, { people }) => adapter.addOne(people, { ...state, loading: false })),
on(fromPeopleAction.CREATE_FAIL, (state, { error }) => ({ ...state, error, loading: false })),
on(fromPeopleAction.DELETE_SUCCESS, (state, { id }) => adapter.removeOne(id, { ...state, loading: false })),
on(fromPeopleAction.DELETE_FAIL, (state, { error }) => ({ ...state, error, loading: false }))
);
请注意,我们有一些“on()
”做同样的事情,是否可以为同一个 on()
调用多个操作?
根据 on
's overloads 判断,如果您这样做,我认为您可以对多个操作使用相同的逻辑:
on(action1, action2, action3, ...action10, (state, action) => {})
请注意,在 10 actions 之后,您将丢失类型。
同样重要的是要注意最后一个参数应该是reducer。