-
-
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
Accept initialState function to allow dynamic initial state in AoT #51
Comments
Seems legit. Want to take a crack at making a PR? |
You can also provide your own initial state through the INITIAL_STATE token. import { INITIAL_STATE } from '@ngrx/store';
// in the providers array
{ provide: INITIAL_STATE, useFactory: getInitialState } |
@brandonroberts That's a great idea, and I'm a bit embarrassed I didn't think to do it. That said, allowing I'm happy to open a PR, but I'm wondering your thoughts. Either a PR documenting how to override the provider (since you're exporting all the tokens), or a PR allowing Thanks |
@bfricka a PR allowing |
@brandonroberts @robwormald This PR should be ready to. It looks like there's perhaps an issue with the |
@bfricka Could you explain how to use initialStateFactory to dynamically get the initial state from the backend? |
This may seem like an odd question, but do you need initial state before the application is loaded? Like, are you trying to re-hydrate state saved on the backend? Because there is an inherent problem with this approach if so: You'd need to delay instantiation of your application until you successfully retrieved the initial state. Otherwise, it's just updating state. Providing initial state implies that you have access to it, and if you don't you probably shouldn't hold up application bootstrap specifically for that purpose (see "Shell Architecture"). All this said, a standard way of providing initial state is described now in the docs. Just FYI, the use case I applied this to was retrieving initial state via |
@bfricka in the official docs, the way the example get the initial state, the const |
@Dunos Yeah, so
Ideally, this could be an asynchronous function, but @ngrx is synchronous. |
Apologies if I'm missing something glaringly obvious but what if I wanted to call a service from within the getInitialState() function? I have an Angular Service that reads a local JSON file and I want to dynamically set a config item stored in the store on load. Is something like that possible?
EDIT - I'm guessing I'd have to do the following (unless I'm missing something) - https://stackoverflow.com/a/41211624 |
I also want to use a service to get/set some initial state. Is there plan to allow injecting service inside reducers? Or do we just manually initialize a service and use it in the reducer? For now, I am manually dispatching some action with payload to update the state during component initialization. |
@maxisam @VinodhSubramanian1193 @bfricka this is little bit to late but maybe it can help you and others when loading from localstore/indexeddb respectively |
This is problem carried over from @ngrx/store (see: 273, 348).
AoT requires static objects in module definitions, so dynamic initial state is only possible with:
or
The text was updated successfully, but these errors were encountered: