intendednull / yewdux

Ergonomic state management for Yew applications
https://intendednull.github.io/yewdux/
Apache License 2.0
319 stars 31 forks source link

No initial state-event for persisted state using a struct component #25

Closed lpotthast closed 2 years ago

lpotthast commented 2 years ago

When using the new Store macro as in https://github.com/intendednull/yewdux/tree/master/examples/persistent, that is, using a function component, everything works as expected.

But when using the persistent store in a struct component with:

Self {
    dispatch: Dispatch::<State>::subscribe(ctx.link().callback(Msg::StateUpdated)),
    state: Default::default(),
    ...

When the component is created with an old state persisted in the browser, that state is not passed to the component with a Msg::StateUpdated message. The state field inside the component struct will just stay to what Default::default() returned. Only a state change happening after the component was created will pass the current state to the component using the message.

intendednull commented 2 years ago

Fixed in 452faf1

Should work now. You can also get the current state directly if needed:

let dispatch = Dispatch::<State>::subscribe(ctx.link().callback(Msg::StateUpdated));
Self {
    state: dispatch.get(),
    dispatch,
}
lpotthast commented 2 years ago

Works perfectly with the change applied.

And thanks for noting the .get function. But Rust yells here when trying to call .get(), as

no method named `get` found for struct `yewdux::dispatch::Dispatch<State>` [...]
the candidate is defined in an impl for the type `yewdux::dispatch::Dispatch<S>[...]
use associated function syntax instead: `yewdux::dispatch::Dispatch::<State>::get`

Using Dispatch::<State>::get() works, but I think I will just stick to using Default::default(), as the state is cloned/replaced either way..

intendednull commented 2 years ago

@inf0rm4tik3r oops! get should be fixed now. There was a couple others like that, plus a bug with Mrc that is fixed too.