Open averichev opened 6 months ago
Thanks for this issue!
For now, there isn't really a way to have centralized state.
You can share states from parents to children (~/examples/nested_store.rs
), but there isn't really a way to have everything centralized.
Tho you could do something like:
fn app() -> Element {
let mut signal: Signal<i32> = use_context();
let mut store = CounterStore::new(CounterStoreProps { count: signal() });
rsx! {
button { onclick: move |_| store.inc(), "+1" }
button { onclick: move |_| store.dec(), "-1" }
"{store.count}"
}
}
Would you like a complete example of that ? I might do a macro for use_context in the future!
As someone who uses Mobx, I usually don't use (Reacts) context at all outside of theme. Mostly because it hides the contract between the component and what it needs. By making sure that things are passed in as props, you know exactly what comes in and what is expected to render a component. You'll find quickly that it isn't as bad as you think it is.
One key ingredient is, that nested data always have a way to get back to the root Application store.
e.g AppStore -> TodoStore -> Todo
Todo
has a weak reference to its store TodoStore
which in return has a weak reference to AppStore
.
The only time I've ever used context is for accessing theme data. But the component can run without a theme, so it doesn't matter. But it can't run without the state it needs, which is why it's important to have it as a prop.
Can i use modx алк shared state pattern? Without set parameters from parent to child component? Like use_context_provider provider?