intendednull/yewdux

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

lpotthast opened this issue · 3 comments

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.

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,
}

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..

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