LeetCode-OpenSource/ayanami

State should be the first param in Reducer

Brooooooklyn opened this issue · 1 comments

First, we should align behavior with redux reducer, or we need change the name Reducer to something else.

The second, in most case, action & payload is optional, there are too many codes in Modules like :

setLoading(_: void, state: State)

too ugly.

Yes, you are right. In this scenario, the syntax _: void looks pretty redundant. It will be great if we can remove it. But if we just simply move state: State to the first, the same things will happen to State too, as Reducer can return a partial State.

setId(_: State, id: number): Partial<State> {
  return { id }
}

So, how about changing the return type of Reducer from "partial state" to "new state"? This way, we don't mutate the state anymore. Instead, we set a new state from the return value of Reducer as redux do.

setId(state: State, id: number): State {
  return { ...state, id }
}