rsnext/packages/next/shared/lib
Sukka 26459ef097
replace use-subscription with use-sync-external-store (#36733)
- [x] Make sure the linting passes by running `yarn lint`

Back in 2019, React released the first version of `use-subscription` (https://github.com/facebook/react/pull/15022). At the time, we only has limited information about concurrent rendering, and #9026 add the initial concurrent mode support.

In 2020, React provides a first-party official API `useMutableSource` (https://github.com/reactjs/rfcs/pull/147, https://github.com/facebook/react/pull/18000):

> ... enables React components to safely and efficiently read from a mutable external source in Concurrent Mode.

React 18 introduces `useMutableSource`'s replacement `useSyncExternalStore` (see details here: https://github.com/reactwg/react-18/discussions/86), and React changes `use-subscription` implementation to use `useSyncExternalStore` directly: https://github.com/facebook/react/pull/24289

> In React 18, `React.useSyncExternalStore` is a built-in replacement for `useSubscription`.
> 
> This PR makes `useSubscription` simply use `React.useSyncExternalStore` when available. For pre-18, it uses a `use-sync-external-store` shim which is very similar in `use-subscription` but fixes some flaws with concurrent rendering.

And according to `use-subscription`:

> You may now migrate to [`use-sync-external-store`](https://www.npmjs.com/package/use-sync-external-store) directly instead, which has the same API as `React.useSyncExternalStore`. The `use-subscription` package is now a thin wrapper over `use-sync-external-store` and will not be updated further.

The PR does exactly that:

- Removes the precompiled `use-subscription` introduced in #35746
- Adds the `use-sync-external-store` to the dependencies.
  - The `use-sync-external-store` package enables compatibility with React 16 and React 17.
  - Do not pre-compile `use-sync-external-store` since it is also the dependency of some popular React state management libraries like `react-redux`, `zustand`, `valtio`, `@xstate/react` and `@apollo/client`, etc. By install
- Replace `useSubscription` usage with `useSyncExternalStore` 

---

Ref: #9026, #35746 and #36159


Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
2022-05-08 12:19:33 +00:00
..
i18n Add initial handling for routing tests (#36635) 2022-05-03 10:37:23 +00:00
isomorphic polish isomorphic path module (#36521) 2022-04-27 18:23:26 +00:00
page-path Tweak routing tests (#36667) 2022-05-05 13:15:32 +02:00
router Tweak routing tests (#36667) 2022-05-05 13:15:32 +02:00
amp-context.ts Move code shared between server/client to "shared" folder (#26734) 2021-06-30 11:43:31 +02:00
amp.ts Move code shared between server/client to "shared" folder (#26734) 2021-06-30 11:43:31 +02:00
constants.ts Tweak routing tests (#36667) 2022-05-05 13:15:32 +02:00
dynamic.tsx Hoist the desired ReactDOM import expression (#36552) 2022-04-28 19:17:23 +00:00
escape-regexp.ts perf(escapeStringRegexp): test before replace (#34472) 2022-04-16 05:42:10 +00:00
flatten.ts Refactor Page Paths utils and Middleware Plugin (#36576) 2022-04-30 11:19:27 +00:00
flush-effects.ts Add unstable_useFlushEffects hook (#34117) 2022-02-18 00:18:28 +00:00
head-manager-context.ts next/script fix duplicate scripts (#28428) 2021-08-24 11:07:38 -05:00
head.tsx Hoist the desired ReactDOM import expression (#36552) 2022-04-28 19:17:23 +00:00
html-context.ts Revert "Revert "Make concurrent features independent from the global runtime option"" (#35490) 2022-03-26 00:05:35 +01:00
image-config-context.ts fix: make sure image-config gets transpiled (#34677) 2022-02-22 09:27:18 -05:00
image-config.ts Add experimental wildcard remotePatterns config for upstream images (#36245) 2022-05-05 02:19:16 +00:00
is-plain-object.ts fix: fixes #33314 move is-plain-object for es5 compilation (#33690) 2022-01-27 17:59:42 +00:00
loadable-context.ts Move code shared between server/client to "shared" folder (#26734) 2021-06-30 11:43:31 +02:00
loadable.d.ts Move code shared between server/client to "shared" folder (#26734) 2021-06-30 11:43:31 +02:00
loadable.js replace use-subscription with use-sync-external-store (#36733) 2022-05-08 12:19:33 +00:00
match-remote-pattern.ts Add experimental wildcard remotePatterns config for upstream images (#36245) 2022-05-05 02:19:16 +00:00
mitt.ts Move code shared between server/client to "shared" folder (#26734) 2021-06-30 11:43:31 +02:00
post-process.ts Remove experimental image optimization feature (#34349) 2022-02-15 01:36:51 +00:00
router-context.ts Add new target for middleware (#30299) 2021-10-26 18:50:56 +02:00
runtime-config.ts Move code shared between server/client to "shared" folder (#26734) 2021-06-30 11:43:31 +02:00
side-effect.tsx Move code shared between server/client to "shared" folder (#26734) 2021-06-30 11:43:31 +02:00
styled-jsx.d.ts Reexports styled-jsx JSXStyle in nextjs (#36585) 2022-04-30 20:25:05 +00:00
styled-jsx.js Reexports styled-jsx JSXStyle in nextjs (#36585) 2022-04-30 20:25:05 +00:00
utils.ts Update status code for normalize error (#36580) 2022-05-01 09:23:06 +00:00