Skip to content

Commit 26459ef

Browse files
SukkaWhuozhi
andauthored
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` (facebook/react#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` (reactjs/rfcs#147, facebook/react#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: reactwg/react-18#86), and React changes `use-subscription` implementation to use `useSyncExternalStore` directly: facebook/react#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>
1 parent 3fd1168 commit 26459ef

File tree

7 files changed

+15
-76
lines changed

7 files changed

+15
-76
lines changed

packages/next/compiled/use-subscription/LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/next/compiled/use-subscription/index.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/next/compiled/use-subscription/package.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/next/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
"@next/env": "12.1.7-canary.3",
7373
"caniuse-lite": "^1.0.30001332",
7474
"postcss": "8.4.5",
75-
"styled-jsx": "5.0.2"
75+
"styled-jsx": "5.0.2",
76+
"use-sync-external-store": "1.1.0"
7677
},
7778
"peerDependencies": {
7879
"fibers": ">= 3.1.0",
@@ -260,7 +261,6 @@
260261
"tty-browserify": "0.0.1",
261262
"ua-parser-js": "0.7.28",
262263
"unistore": "3.4.1",
263-
"use-subscription": "1.5.1",
264264
"util": "0.12.4",
265265
"uuid": "8.3.2",
266266
"vm-browserify": "1.1.2",

packages/next/shared/lib/loadable.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
2222
// Modified to be compatible with webpack 4 / Next.js
2323

2424
import React from 'react'
25-
import { useSubscription } from 'next/dist/compiled/use-subscription'
25+
import { useSyncExternalStore } from 'use-sync-external-store/shim'
26+
2627
import { LoadableContext } from './loadable-context'
2728

2829
const ALL_INITIALIZERS = []
@@ -75,6 +76,7 @@ function createLoadableComponent(loadFn, options) {
7576
opts.lazy = React.lazy(opts.loader)
7677
}
7778

79+
/** @type LoadableSubscription */
7880
let subscription = null
7981
function init() {
8082
if (!subscription) {
@@ -116,7 +118,12 @@ function createLoadableComponent(loadFn, options) {
116118
init()
117119

118120
const context = React.useContext(LoadableContext)
119-
const state = useSubscription(subscription)
121+
const state = useSyncExternalStore(
122+
subscription.subscribe,
123+
subscription.getCurrentValue,
124+
subscription.getCurrentValue
125+
)
126+
120127
React.useImperativeHandle(
121128
ref,
122129
() => ({

packages/next/taskfile.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -306,24 +306,6 @@ export async function ncc_react_refresh_utils(task, opts) {
306306
}
307307
}
308308

309-
// eslint-disable-next-line camelcase
310-
export async function ncc_use_subscription(task, opts) {
311-
await task
312-
.source(
313-
opts.src || relative(__dirname, require.resolve('use-subscription'))
314-
)
315-
.ncc({
316-
packageName: 'use-subscription',
317-
externals: {
318-
...externals,
319-
react: 'react',
320-
'react-dom': 'react-dom',
321-
},
322-
target: 'es5',
323-
})
324-
.target('compiled/use-subscription')
325-
}
326-
327309
// eslint-disable-next-line camelcase
328310
externals['chalk'] = 'next/dist/compiled/chalk'
329311
export async function ncc_chalk(task, opts) {
@@ -1652,7 +1634,6 @@ export async function ncc(task, opts) {
16521634
'ncc_node_html_parser',
16531635
'ncc_watchpack',
16541636
'ncc_chalk',
1655-
'ncc_use_subscription',
16561637
'ncc_napirs_triples',
16571638
'ncc_etag',
16581639
'ncc_p_limit',

yarn.lock

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21248,12 +21248,10 @@ url@^0.11.0:
2124821248
punycode "1.3.2"
2124921249
querystring "0.2.0"
2125021250

21251-
use-subscription@1.5.1:
21252-
version "1.5.1"
21253-
resolved "https://registry.yarnpkg.com/use-subscription/-/use-subscription-1.5.1.tgz#73501107f02fad84c6dd57965beb0b75c68c42d1"
21254-
integrity sha512-Xv2a1P/yReAjAbhylMfFplFKj9GssgTwN7RlcTxBujFQcloStWNDQdc4g4NRWH9xS4i/FDk04vQBptAXoF3VcA==
21255-
dependencies:
21256-
object-assign "^4.1.1"
21251+
use-sync-external-store@1.1.0:
21252+
version "1.1.0"
21253+
resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.1.0.tgz#3343c3fe7f7e404db70f8c687adf5c1652d34e82"
21254+
integrity sha512-SEnieB2FPKEVne66NpXPd1Np4R1lTNKfjuy3XdIoPQKYBAFdzbzSZlSn1KJZUiihQLQC5Znot4SBz1EOTBwQAQ==
2125721255

2125821256
use@^3.1.0:
2125921257
version "3.1.1"

0 commit comments

Comments
 (0)