intendednull / yewdux

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

Add PartialEq to StoreRef #31

Closed matanmarkind closed 2 years ago

matanmarkind commented 2 years ago

It would be great if StoreRef supported PartialEq since this would allow for using the store as the dependency in use_effect_with_deps. I am using yewdux_function 0.1 and to get around this I used:

pub struct StoreWatcher {
    store_ref: StoreRef<PersistentStore<YewduxStore>>,
}

impl PartialEq for StoreWatcher {
    fn eq(&self, other: &Self) -> bool {
        self.store_ref.state() == other.store_ref.state()
    }
}

let store_watcher = StoreWatcher {
    store_ref: use_store::<PersistentStore<YewduxStore>>(),
};

use_effect_with_deps(||(), store_watcher);

This assumes that I can store a StoreRef this way to check on repeatedly. This seemed to work in my code where the first run was occuring with store.state() returning None, so at least in my case that seems to hold.

intendednull commented 2 years ago

StoreRef has been removed, on master you now get a direct Rc of your state.

Your solution seems like a sound workaround. You could also add just the state as a dependency, instead of the entire StoreRef. Something like use_effect_with_deps(..,store_ref.state().map(Rc::clone))

If the issue is pressing enough, I wouldn't be opposed to a quick patch for 0.7, however master is already much improved, just waiting on next Yew version to release.

matanmarkind commented 2 years ago

Thanks for the response and your idea, definitely more convenient than what I was doing.

I don't think this is very pressing, just a nice to have for a future version (which is now irrelevant).