When in a reducer i do:
export const initialState: QuestionsState = (() => {
const defaultInitialState = adapter.getInitialState();
return adapter.addMany(answeredQuestions, defaultInitialState);
})();
In angular production mode using the adapter.addMany on module initialization I get an error: "Error: cannot enable prod mode after platform setup."
The adapter should not be using the isDevMode, using that before the application bootstrap is complete gives this error.
Chrome Version 70.0.3538.110, npm v6.1.0, node v10.3.0, ngrx v6.1.2
[ ] Yes (Assistance is provided if you need help submitting a pull request)
[x] No
Hi, do you got a reproduction of this behavior?
What do you mean by reproduction ? If you mean stackblitz or something, no sorry. If you want that I can provide it in a few days, but it's pretty easy to test...
Yes, a stacklitz would be ideal.
It would be great if you could provide it.
I've taken a look into this, the solution is to invoke the initial state function from inside the reducer instead of calling it immediately. So the invocation of adapter.addMany is postponed until Angular is bootstrapped.
The code would look like:
export const initialState: QuestionsState = (() => {
const defaultInitialState = adapter.getInitialState();
return adapter.addMany(answeredQuestions, defaultInitialState);
});
export function reducer(state = initialState(), action: Action) {
return state;
}
If this doesn't solve the problem, feel free to re-open the issue.
Sorry for the delay for the stackblitz example, end of the year is tough, was just about to do it today, but I saw you wrote. Thanks for the solution. I workaround it almost the same way, but my code was in the reducer body. Yours is more elegant.
Still, do you think working around the issue this way is best ?
I think this isn't an issue but is intended this way.
Also I don't know where answeredQuestions comes from, if it's from local storage it would be better to do this in a meta-reducer - where you won't encounter the issue, initial state should be pure imho.
@timdeschryver I've encountered this case in my project and I've solved it the same way you provided. However, I'm interested in the idea with meta-reducer. Could you point me to some tutorial explaining loading initialState using meta-reducer?
@Nuurek I keep my initial state pure.
If I want to rehydrate my state on the initial load I use a meta-reducer.
You can take a look here for an example.