Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need example for shared state pattern #3

Open
averichev opened this issue May 16, 2024 · 2 comments
Open

Need example for shared state pattern #3

averichev opened this issue May 16, 2024 · 2 comments

Comments

@averichev
Copy link

Can i use modx алк shared state pattern? Without set parameters from parent to child component? Like use_context_provider provider?

@tkr-sh
Copy link
Owner

tkr-sh commented May 16, 2024

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!

@seivan
Copy link

seivan commented Aug 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants