This feature request is for "@ngxs/store": "^3.5.0"
It would be great if we could get errors/warnings when we have two states with the same action type and there should be a flag for disabling it so that we can disable it in production.
Could you give an example? What do you mean by "two states with the same action type"?
``` import { Component } from '@angular/core';
import { Store, Select } from '@ngxs/store';
import { Observable } from 'rxjs';
import { State, Action } from '@ngxs/store';
@State<{number: {
value
}}>({
name: 'count2'
})
export class CountState1 {
@Action({ type:"some action"})
add({ getState, setState }) {
console.log("countState1");
}
}
@State<{number: {
value
}}>({
name: 'count1',
})
export class CountState2 {
@Action({ type:"some action"})
add({ getState, setState }) {
console.log("countState2");
}
}
@Component({
selector: 'my-app',
template:
<button (click)="onClick()">Click Me</button>
})
export class AppComponent {
constructor(private store: Store) {}
onClick() {
this.store.dispatch({ type:"some action"});
}
}
```
Here as you can see, we have two states. They receive one action some action and handle them. So when you dispatch an event of type some action both states receive the event.
This is by design and this SHOULD be possible to declare action in different states. E.g. you've got an action with reset type that is handled in 3 states.
I also don't understand the motivation to name actions equally in the above case. We've got a style guide page in our docs where we say how to name actions via [StateName] ActionDescription.
This is by design and this SHOULD be possible to declare action in different states. E.g. you've got an action with reset type that is handled in 3 states.
What happened to me was that I accidentally created two actions with the same name and then while dispatching two states were getting updated. It took me long to figure it out. That's why I thought it would have been a better idea if we had a check for this. But now after giving it a thought it makes sense to me.
@shivamd20
That's the point of most style guides and naming convetions. They provide you the ability to prevent such errors. I would tell you frankly I have developed several huge apps ugins NGXS and also taught developers, and we've never had such situations.
What happened to me was that I accidentally created two actions with the same name and then while dispatching two states were getting updated.
I cannot concur and I cannot argue. I would be neutral in this question as you know "even a stopped clock gives the right time twice a day" :joy:
Just follow the naming convetion and you'll be alright! :)
P.S. I'm open to any following questions if you still have.
Most helpful comment
This is by design and this SHOULD be possible to declare action in different states. E.g. you've got an action with
resettype that is handled in 3 states.