0
votes

I am just starting with ngrx/store in an Angular-2-Project. My question concerns the distribution of actions through the dispatch-method of the store: Will the action be passed through all reducers? That would mean that the action-type would have to be globally unique? Is this correct?

I am asking this question because in some tutorials the types of the actions are quite short strings, as if they were somewhat locally limitted. But in case they are global, one should have some sort of prefix_namespacing, right?

Thanks!

2

2 Answers

1
votes

Yes it's correct that action types need to be unique.

If you check the ngrx example it uses an action creator pattern that prefixes the actions with the name of the action type.

https://github.com/ngrx/example-app/blob/master/src/app/actions/book.ts

1
votes

Yes, the store combines all your reducers into one and so will effectively dispatch an action to all your reducers. If many reducers have the matching action they will all execute the code they have for that action.

One way of making your actions unique is to expose them through classes rather than string constants directly, there's an example of this here:

https://github.com/ngrx/example-app/blob/master/src/app/reducers/books.ts

You can see how the actions are defined in app/actions