Skip to content

Commit 85de85f

Browse files
committed
Add quick useSyncExternalStore shim
1 parent 0579cfc commit 85de85f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
}
5050
},
5151
"peerDependencies": {
52-
"react": "^16.8.0 || ^17.0.0"
52+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
5353
},
5454
"devDependencies": {
5555
"@ampproject/rollup-plugin-closure-compiler": "^0.26.0",

src/internals/dispatcher.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,22 @@ function useDeferredValue<T>(input: T): T {
320320
return input
321321
}
322322

323+
// See: https://github.com/facebook/react/blob/fe41934/packages/use-sync-external-store/src/useSyncExternalStoreShimServer.js#L10-L20
324+
function useSyncExternalStore<T>(
325+
subscribe: (() => void) => () => void,
326+
getSnapshot: () => T,
327+
getServerSnapshot?: () => T
328+
): T {
329+
// Note: The shim does not use getServerSnapshot, because pre-18 versions of
330+
// React do not expose a way to check if we're hydrating. So users of the shim
331+
// will need to track that themselves and return the correct value
332+
// from `getSnapshot`.
333+
return getSnapshot()
334+
}
335+
323336
export const Dispatcher = {
324337
readContext,
338+
useSyncExternalStore,
325339
useContext,
326340
useMemo,
327341
useReducer,

0 commit comments

Comments
 (0)