Skip to content
This repository has been archived by the owner on Jan 10, 2018. It is now read-only.

Dispatching an action in constructor of root component doesn't fire #207

Closed
firecube opened this issue Sep 7, 2016 · 5 comments
Closed

Comments

@firecube
Copy link

firecube commented Sep 7, 2016

Hi,

In my root app component I have the following in my constructor but my reducer function is never hit.

this.store.dispatch(this.dataActions.doStuff(args));

Moving the same code to a 'page level' routed component works fine as does wrapping the above in a setTimeout like this...

setTimeout(() => {
this.store.dispatch(this.dataActions.doStuff(args));
}, 0);

Thanks, Simon

@awerlang
Copy link

awerlang commented Sep 8, 2016

From your description, your reducers are being registered after the app component is instantiated. This is different from what I experience, my reducers are in feature modules, which are imported in the main module, so they are provided to the store before the main component is instantiated and bootstraped.

If you haven't upgraded to angular rc6, I suggest you to keep the workaround until you're able to migrate to latest angular/ngrx versions.

@firecube
Copy link
Author

firecube commented Sep 8, 2016

Thanks for the suggestion but this isn't the case.

I am on rc.6 already. @ngrx modules are all imported in my app module and my app component is set as the bootstrap component.

I am using @ngrx/store, @ngrx/effects, @ngrx/router-store, @ngrx/store-devtools and @ngrx/store-log-monitor. My store is initialised in my app module with StoreModule.provideStore(reducer). The 'reducer' is a compose(combineReducers)({...}) call where by reducers are set up.

Other calls are working in the rest of the app apart from this one in the constructor or my root component.

Any other ideas?

Thanks, Simon

@brandonroberts
Copy link
Member

Are you still experiencing this issue?

@firecube
Copy link
Author

Hi,

Yes, I am still seeing this issue. If I dispatch an action in my app constructor it does not hit my reducer or show up in the ngrx dev tools. I am using this to pre-load some data so the reducer sets a loading flag and I have an effect which makes an http call. Interestingly the effect does pick up the action and my data is loaded.

In my app I have changed my effect to use startWith after I saw it in one of your samples which is a nicer approach but this does seem like a bug to me.

Thanks, Simon

@robwormald
Copy link
Contributor

Please check this against NgRx v4, and if it’s still an issue, please reopen on https://github.com/ngrx/platform. Thanks!

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

No branches or pull requests

5 participants