Platform: Multiple feature modules requires unique action names

Created on 16 Aug 2018  路  5Comments  路  Source: ngrx/platform

I'm using the latest available version 6.1.0 with Angular 6.0.3.

I looked around and could not find an answer to this. The issue I'm having is when I use multiple feature modules that have the same action name - when I trigger an action in one feature module it changes the state in the other feature module as well.

For ex.

1)

Feature Module 1 has action: SetName
Feature Module 2 has action: SetName

2)

Trigger Module 1 SetName, for ex:

this.store.dispatch(new Module1Actions.SetName('Mike'));

3) Check the state in Redux DevTools, name for Module1 and Module2 are updated to the same value set in the previous step.

Most helpful comment

While it may seem like a good idea, I think it isn't.
This would prevent good action hygiene.

All 5 comments

Use a prefix for all actions in that module and/or use case

Exactly what @dummdidumm says. All reducers are invoked with the dispatched acation, meaning if you use the same action type it will trigger both changes. To solve this use a prefix, e.g. [Feat1 DetailsPage] SetName and [Feat2 OverviewPage] SetName.

In other words, your action types must be unique.

@dummdidumm
@timdeschryver

Thanks for the help!

If I understand correctly using a prefix would be a manual process for each action, I will go ahead with that. But It would be nice to have a global prefix you can register to each feature module that will add the prefix to each action automatically - just a thought.

While it may seem like a good idea, I think it isn't.
This would prevent good action hygiene.

@timdeschryver thanks for that link. I just watched that video and learned a few things which I will apply to my project.

The way I was thinking about it regarding automatically applying a prefix by feature module could be thought of as an additional to what was described in the video. For example, if your actions are like [Feat1 DetailsPage] SetName, [Feat2 OverviewPage] SetName, the feature prefix of the action could simply be appended from what is registered once since it is going to be the same always for that feature module.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

itprodavets picture itprodavets  路  3Comments

bhaidar picture bhaidar  路  3Comments

sandangel picture sandangel  路  3Comments

hccampos picture hccampos  路  3Comments

doender picture doender  路  3Comments