Skip to content

Commit

Permalink
Obfuscate React.useSyncExternalStore to prevent webpack complaint.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed May 11, 2022
1 parent 293af71 commit 7a531df
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/react/hooks/useSyncExternalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ 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 obfuscatedReactNamespace: typeof React & {
useSyncExternalStore?: RealUseSESHookType;
} = React;

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

Expand All @@ -21,9 +28,7 @@ export const useSyncExternalStore: RealUseSESHookType = (
getServerSnapshot,
) => {
// When/if React.useSyncExternalStore is defined, delegate fully to it.
const realHook = (React as {
useSyncExternalStore?: RealUseSESHookType;
}).useSyncExternalStore;
const realHook = obfuscatedReactNamespace.useSyncExternalStore;
if (realHook) {
return realHook(subscribe, getSnapshot, getServerSnapshot);
}
Expand Down

0 comments on commit 7a531df

Please sign in to comment.