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 refinements #9709

Merged
merged 4 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
},
"dependencies": {
"@graphql-typed-document-node/core": "^3.1.1",
"@types/use-sync-external-store": "^0.0.3",
"@wry/context": "^0.6.0",
"@wry/equality": "^0.5.0",
"@wry/trie": "^0.3.0",
Expand Down Expand Up @@ -109,6 +108,7 @@
"@types/node": "16.11.33",
"@types/react": "17.0.45",
"@types/react-dom": "17.0.16",
"@types/use-sync-external-store": "^0.0.3",
"acorn": "8.7.1",
"bundlesize": "0.18.1",
"cross-fetch": "3.1.5",
Expand Down
19 changes: 9 additions & 10 deletions src/react/hooks/useSyncExternalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ type RealUseSESHookType =
// when only React 17 or earlier is installed.
typeof import("use-sync-external-store").useSyncExternalStore;

// Prevent webpack from complaining about our feature detection of the
// useSyncExternalStore property of the React namespace, which is expected not
// to exist when using React 17 and earlier, and that's fine.
const uSESKey = "useSyncExternalStore" as keyof typeof React;
const realHook = React[uSESKey] as RealUseSESHookType | undefined;

// Adapted from https://www.npmjs.com/package/use-sync-external-store, with
// Apollo Client deviations called out by "// DEVIATION ..." comments.

export const useSyncExternalStore: RealUseSESHookType = (
// When/if React.useSyncExternalStore is defined, delegate fully to it.
export const useSyncExternalStore: RealUseSESHookType = realHook || ((
subscribe,
getSnapshot,
getServerSnapshot,
) => {
// When/if React.useSyncExternalStore is defined, delegate fully to it.
const realHook = (React as {
useSyncExternalStore?: RealUseSESHookType;
}).useSyncExternalStore;
if (realHook) {
return realHook(subscribe, getSnapshot, getServerSnapshot);
}
Comment on lines -18 to -29
Copy link
Member Author

Choose a reason for hiding this comment

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

I think you'll appreciate this change @brainkim


// Read the current snapshot from the store on every render. Again, this
// breaks the rules of React, and only works here because of specific
// implementation details, most importantly that updates are
Expand Down Expand Up @@ -112,7 +111,7 @@ export const useSyncExternalStore: RealUseSESHookType = (
}, [subscribe]);

return value;
}
});

function checkIfSnapshotChanged<Snapshot>({
value,
Expand Down