Reputation: 1576
I am updating to ngrx 8, and I noticed that in the reducer
function the type of the state
parameter can be either State
or undefined
https://ngrx.io/guide/store/reducers#creating-the-reducer-function
export function reducer(state: State | undefined, action: Action) {
return scoreboardReducer(state, action);
}
Is there any reason for the undefined
optional type?
Upvotes: 1
Views: 1527
Reputation: 15487
The first time that the reducers get invoked it's with an undefined
state.
This is done, so you're able to provide a default value for the state.
Upvotes: 1
Reputation: 18809
My opinion is that it is for type checking for default parameters. To set the initialState, you can do
export function reducer(state: State | undefined = {}, action: Action) {
return scoreboardReducer(state, action);
}
Check out the = {}
. If state
is undefined, it will be equal to {}
.
You can set the initialState
when undefined or just leave it.
Upvotes: 0