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

useStore stops firing on state changes after component remounts #86

Closed
karlpetersson opened this issue Jan 28, 2020 · 9 comments · Fixed by #91
Closed

useStore stops firing on state changes after component remounts #86

karlpetersson opened this issue Jan 28, 2020 · 9 comments · Fixed by #91

Comments

@karlpetersson
Copy link

karlpetersson commented Jan 28, 2020

When switching between mounting two components that both calls the useStore hook, where one of them conditionally mounts a child that also calls useStore, the parent will stop rendering on state changes after remounting. Here's a minimal example on codesandbox:

https://codesandbox.io/s/nostalgic-hooks-0x63r

Repro steps are included. Let me know if you want the code pasted here instead.

Could this be related to #85 ?

edit: updated codesandbox link, messed around in the original one I posted without realizing it.

@karlpetersson
Copy link
Author

Downgrading to 2.2.1 seems to fix this issue. Tested by downgrading the zustand package in the above codesandbox.

@karlpetersson
Copy link
Author

karlpetersson commented Jan 29, 2020

Dug in a little and it seems that subscribers are indeed being overwritten like in #84, in this case the child gets assigned same index as its parent.

What about compressing the subscribers array right after adding a subscriber? This seems to solve this issue as well as work with #84, while keeping order/indices intact. E.g: Below does not work as is.

const subscribe: Subscribe<TState> = <StateSlice>(
    subscriber: Subscriber<TState, StateSlice>
  ) => {
    // ...
    subscribers[subscriber.index] = subscriber;

    subscriberCount = 0;
    subscribers = subscribers.filter(s => {
      subscriber.index = subscriberCount++;
      return true;
    });

    return () => delete subscribers[subscriber.index];
  };

@Jamikk
Copy link

Jamikk commented Feb 3, 2020

I have the same problem

@fzkirablackwhy
Copy link

me too

@kasaley
Copy link

kasaley commented Feb 6, 2020

me too! HELP ZUSTAND TEAM

@dentrado
Copy link

Any news on this? Seems to still be a problem

@kasaley
Copy link

kasaley commented Feb 19, 2020

@dentrado On version 2.2.1, everything is fine

@dentrado
Copy link

But then I get the bug they fixed in 2.2.2 instead

@kasaley
Copy link

kasaley commented Feb 19, 2020

@dentrado very sadly (

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

Successfully merging a pull request may close this issue.

5 participants