Skip to content
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

provideState(feature,reducers) throws an exception when used after StoreModule.forFeature(...) #4063

Closed
1 of 2 tasks
DaveMBush opened this issue Oct 9, 2023 · 4 comments
Closed
1 of 2 tasks

Comments

@DaveMBush
Copy link

DaveMBush commented Oct 9, 2023

Which @ngrx/* package(s) are the source of the bug?

store

Minimal reproduction of the bug/regression with instructions

https://stackblitz.com/edit/demo-of-ngrx-bug?file=src%2Fapp%2Fshared%2Fshared.module.ts

When you run the application, it throws an exception. on line 1343 of ngrx-store.mjs. It seems that featureReducers and features are out of sync. It does not matter what order I provide the new features in.

Also it does not matter if I use StoreModule.forFeature() in the imports section or if I use it inside importProvidersFrom().

If I add two features using StoreModule.forFeature() it works as expected. No exceptions and the store is usable.

As the error is evident without selectors or actions I've created the bare minimum code (no actions or selectors) to produce the error in the stackblitz

Expected behavior

No exception and the new feature store is added.

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)

NgRX 16.3.0
Angular 16.2.8

Other information

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No
@brandonroberts
Copy link
Member

Are you importing these modules in the AppModule this way? It's not intended behavior to import StoreModule into a component's imports.

@timdeschryver
Copy link
Member

Good catch @brandonroberts , I didn't notice that it was provided via the component.
Seems to work when they're imported within a module.

@DaveMBush
Copy link
Author

DaveMBush commented Oct 13, 2023

Am I to understand that NgRX will only support both ways (StoreModule.forFeature() and provideStore()) if the app component is not a standalone component? But, if there is an App Module, you can have a mix of standalone components and module based components under it?

If this is so, I don't understand why it is the provideStore() that isn't working.

@brandonroberts
Copy link
Member

@DaveMBush essentially, yes. If you're using a standalone setup, I'd expect you to only use standalone APIs for NgRx.

Using StoreModule.forFeature and provideState together is more for compatibility with primarily module-based setups.

The reason is doesn't work is because a component doesn't do anything with EnvironmentProviders, which is what the standalone APIs use to perform some init logic when loaded. We used to do this in the constructor of an NgModule, but with standalone APIs, the ENVIRONMENT_INITIALIZER is what's used, and has no effect in Component.providers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants