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

useSyncExternalStore should not execute a function returned by getSnapshot #3632

Closed
1 task
birtles opened this issue Jul 26, 2022 · 0 comments · Fixed by #3633
Closed
1 task

useSyncExternalStore should not execute a function returned by getSnapshot #3632

birtles opened this issue Jul 26, 2022 · 0 comments · Fixed by #3633

Comments

@birtles
Copy link

birtles commented Jul 26, 2022

  • Check if updating to the latest Preact version resolves the issue

Describe the bug
I was debugging an issue in Zustand over in pmndrs/zustand#1137 and it looks like the issue stems from Preact's implementation of useSyncExternalStore:

export function useSyncExternalStore(subscribe, getSnapshot) {
  const [state, setState] = useState(getSnapshot);

  // TODO: in suspense for data we could have a discrepancy here because Preact won't re-init the "useState"
  // when this unsuspends which could lead to stale state as the subscription is torn down.

  useEffect(() => {
    return subscribe(() => {
      setState(getSnapshot());
    });
  }, [subscribe, getSnapshot]);

  return state;
}

Specifically, if getSnapshot returns a function, setState will execute it as per the usual behavior of setState when passed a function.

However, the documentation for getSyncExternalStore doesn't say anything about executing a function returned from getSnapshot nor does the shim version appear to do that so I can only guess that this is a bug in Preact.

To Reproduce

Sorry, I haven't had a chance to set up a minimal repro yet.

Expected behavior

It should not execute a function returned by getSnapshot().

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.

1 participant