RFC - NGXS Local/Component state #1979
Replies: 5 comments 3 replies
-
By reading the RFC and by trying to further understand that, I tried to answer these questions:
If these answers are correct, how about having an API something like this: @Component({
selector: 'my-todos',
providers: [TodosState],
...
})
export class TodosComponent implements OnInit {
// The todosState should be responsible to query the `todos`
todos$ = this._todosState.todos$;
constructor(private _todosState: TodosState, private _store: Store) {
// I would use the _store to interact with the global state
}
ngOnInit() {
// The `todosState` should load the `todos` and update the local state as well.
this._todosState.loadTodos('Personal');
}
} I feel that the API |
Beta Was this translation helpful? Give feedback.
-
i like the provide states approach
regarding scope, although is a nice idea i think might be a bit too much overhead for a developer to use it, my unanswered question here are:
|
Beta Was this translation helpful? Give feedback.
-
Without scoped actions we would reload all of the "local state" instances Todos by dispatching If I understand something wrong - I am sorry 👍 |
Beta Was this translation helpful? Give feedback.
-
This lib (https://github.com/microsoft/redux-micro-frontend) has the notion of global state and isolation that play nicely for MFE, wonder if the idea can be ported over. |
Beta Was this translation helpful? Give feedback.
-
Hello, I have some ideas about this feature.
So, what about if we will use
I reckon it might be better solution because:
I like this options more:
or even better
|
Beta Was this translation helpful? Give feedback.
-
A recent improvement in Angular opens the door for us to be able to introduce a locally scoped state concept in NGXS.
The specific improvement that enables this is the
inject
function. This would allow a selector to be able to access a store in the context of a component.If we provide a mechanism to specify one or many States that are provided in just one part of the component tree (and override the
Store
with a store that checks these local states before looking up the component tree), then we could have a locally scoped state(s).The
key
of a State is actually just syntax sugar for how it might be rendered on an imaginary global state object, but this does not mean that this key cannot be available only for a specific part (or parts) of the component tree.A few ideas are being bounced around on what this could look like.
We are seeking proposals of ideas from the community.
Initial Proposal
For the purpose of this example let's say that we have the following API for providing a state at the component level:
Here is an example usage:
Here are some details on how this Component State would be treated
Store
injected into a parent or sibling component. It is only accessible by aStore
injected at this component or at any child component.How would actions be treated?
Store
would be accessible by any state in the application, no matter if they are at a root, feature or component level. There are no "scoped" actions as a part of this proposal.How would selectors be treated?
store.select(...)
at the level of this component or its children would have access to the component state.store.select(...)
outside of this component or its children would not have access to the component state.Store
instance.Now that we have defined the basics of the way that this would behave, we need to define what this API would look like.
We have a few options here, and welcome additional proposals from the community.
Here are some options for consideration (this list may grow as other proposals are made):
Component state provider function naming
A provider function is used to declare and scope a State to the component
Within this option there are a few options for names:
provideNgxsComponentState(states: StateClass[])
provideNgxsComponentStates(states: StateClass[])
provideNgxsLocalState(states: StateClass[])
provideNgxsLocalStates(states: StateClass[])
Alternative - Scope isolation concept
The idea here is that at the component level, an isolated scope can be declared, resulting in all states added at this component or below to be isolated from the parent store scope. NOTE: This concept in itself could be its own RFC, but this is just a seeding of the idea and how it could relate to component level state.
There are a few options for how this could be defined:
There are actually a number of ways that this idea can extend. For example:
scopes
Symbol in their definition (or through aScopedAction
base class).store.dispatch([ new LoadTodos("MyScope") ])
store.dispatch("MyScope", [new LoadTodos()] )
The ideas with this could go on and on, but we would need to decide if this would belong in the core library, what level of complexity it adds, and what the specific use cases are.
If you would like to submit your own proposal
Please create an 'Answer' to this discussion with your proposal which can then be discussed in the thread for it.
Please ensure that it clearly states the following aspects of your proposal:
Please also take the time to look at other proposals and engage in respectful discussion.
We will consider every proposal. Please do not be offended if yours is not accepted.
We want to keep the core of NGXS as simple and maintainable as possible, and sometimes bigger ideas are better as plugins.
Beta Was this translation helpful? Give feedback.
All reactions