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

Remove useSyncExternalStore in favor of useState #10377

Merged
merged 4 commits into from Apr 21, 2023

Conversation

brophdawg11
Copy link
Contributor

@brophdawg11 brophdawg11 commented Apr 20, 2023

Sibling Remix PR: remix-run/remix#6121

Seems useSyncExternalStore wasn't the right solution all along, since it can cause updates to occur in unexpected orders. A minimal reproduction of the underlying issue is here: https://codesandbox.io/s/use-sync-external-store-loop-9g7b81

But basically when using useSyncExternalStore to sync some external state (as we were doing to sync the @remix-run/router state), the following does not work as one would expect. The externalStore.increment() causes state to update and re-run the effect prior to shouldIncrement=false being flushed, so we end up in an infinite loop.

React.useEffect(() => {
  if (shouldIncrement) {
    setShouldIncrement(false);
    externalStore.increment();
  }
}, [state, shouldIncrement]);

@jacob-ebey and I's hunch is that these "sync" updates are flushed synchronously and thus they go ahead of normal useState updates that run asynchronously in concurrent mode.

This is resolved by using local react state to "sync" the external state:

let [state, setState] = React.useState(
  externalStore.getState.bind(externalStore)
);
React.useEffect(() => {
  return externalStore.subscribe((newState) => {
    if (newState !== state) {
      setState(newState);
    }
  });
}, [state]);

This seems to be the recommendation in the react docs as well:

Screenshot 2023-04-20 at 4 36 01 PM

Closes: remix-run/remix#6072

@changeset-bot
Copy link

changeset-bot bot commented Apr 20, 2023

🦋 Changeset detected

Latest commit: f3fd217

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
react-router Patch
react-router-dom Patch
react-router-dom-v5-compat Patch
react-router-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

});
testWindow = dom.window as unknown as Window;
testWindow.history.pushState({}, "", "/");
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reusing the same window instance across these tests was. ... not smart on my part :)

package.json Outdated
},
"packages/react-router/dist/umd/react-router.production.min.js": {
"none": "15.6 kB"
"none": "15.0 kB"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a bonus we drop the use-sync-external-store/shim and shave a few bytes

@brophdawg11 brophdawg11 merged commit 3efa5d0 into dev Apr 21, 2023
2 checks passed
@brophdawg11 brophdawg11 deleted the brophdawg11/remove-uses branch April 21, 2023 20:38
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.11.0-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.11.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants