-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: Add more explanation that you can only have one distinct action type per on function with createReducer #1956
Comments
If you mean that your using the same action in one reducer, then yes you're right (this also true for a reducer with switch cases). const reducer1 = createReducer(
0,
on(addCount, state => state + 1),
on(addCount, state => state + 1)
); If you listen to the same action in multiple const reducer1 = createReducer(
0,
on(addCount, state => state + 1)
);
const reducer2 = createReducer(
0,
on(addCount, state => state + 1)
); In the example above, both reducers will react to the action and update there state. |
Thanks for confirming. It's just not intuitive with the "on" keyword that we know for events coming from multiple frameworks. I was actually doing: const reducer1 = createReducer(
initialState,
on(addCount, state => state.adds + 1),
on(suctractCount, state => state.substracts - 1),
on(addCount, subtractCount, state => {
if (state.adds > 0 && state.substracts < 0) {
return { ... state, hasRun: true };
}
return state;
})
); Granted, this example is quite contrived and can be converted to a selector like I mentioned earlier. But, because it is not explained and fail silently, it is not simple to debug. |
I agree, we all know how switch statements will work, but when using the createReducer method, it's not intuitive. Two options I can think of:
for (let on of ons) {
for (let type of on.types) {
if (isDevMode() && map.has(type)) {
console.warn('message with remediation step')
}
map.set(type, on.reducer);
}
} |
@jtcrowson unfortunetaly we can't add a |
@timdeschryver is there any way to infer whether the app is in production mode? |
Ah okay got it, it's because the reducers get created before |
Yes exactly @jtcrowson, I'm not sure if there's another way to know if it's a prod build or not. This can be added to the documentation, like you proposed. |
Does it matter if prod or dev. It is an error. I think it is justified to print the message so it can be resolved. |
Maybe we should indeed have the error in regardless prod or dev. Someone on my team ran into a similar issue - while transitioning onto new Action Creators, they copy-pasted and duplicated the action |
What about wrapping the |
@timdeschryver That's an interesting approach. Also note that this issue is now explained in the docs (#1980). |
I agree that #1980 explains this more clearly now. I think it would be valuable to have a console error throw in @alex-okrushko example where the user accidentally duplicates. |
Fyi, I'm listing some typescript rules that are useful (in my opinion) with NgRx. |
@timdeschryver @alex-okrushko
And I use it like this way
However, the getUserSuccessAction overwrites the previous reducer. In my opinion, it doesn't really make sense to overwrite (it's error-prone this way). Instead, it should incrementally apply reducers regardless of the action type. I have experimented by changing the source code to the following and it works very well.
Happy to discuss. :) |
It is possible to have 2 effects looking at one action. But it seems impossible to have 2
ons
looking at one action.platform/modules/store/src/reducer_creator.ts
Line 189 in b07ae4e
It is not obvious and it will override my previous action reducer.
After thinking about it, a selector combining 2 variables will do the job, but it's just not obvious and it does fail quite silently!
The text was updated successfully, but these errors were encountered: