|
1 | 1 | // Simplified fork of the useSyncExternalStore hook from the official package,
|
2 | 2 | // with `getServerSnapshot` parameter removed and `instRef` simplified using a symbol.
|
3 |
| -// https://github.com/facebook/react/blob/be8aa76873e231555676483a36534bb48ad1b1a3/packages/use-sync-external-store/src/useSyncExternalStoreWithSelector.js |
| 3 | +// https://github.com/facebook/react/blob/8d74e8c73a5cc5e461bb1413a74c6b058c6be134/packages/use-sync-external-store/src/useSyncExternalStoreWithSelector.js |
4 | 4 | // MIT Licensed https://github.com/facebook/react/blob/be8aa76873e231555676483a36534bb48ad1b1a3/LICENSE
|
5 | 5 |
|
6 | 6 | import { useDebugValue, useEffect, useMemo, useRef, useSyncExternalStore } from 'react'
|
@@ -54,10 +54,13 @@ export function useSyncExternalStoreWithSelector<Snapshot, Selection>(
|
54 | 54 | // The snapshot has changed, so we need to compute a new selection.
|
55 | 55 | const nextSelection = selector(nextSnapshot)
|
56 | 56 |
|
57 |
| - // Use the provided isEqual function to check if the data has changed. If |
58 |
| - // it hasn't, return the previous selection. That signals to React that |
59 |
| - // the selections are conceptually equal, and we can bail out of rendering. |
| 57 | + // If a custom isEqual function is provided, use that to check if the data |
| 58 | + // has changed. If it hasn't, return the previous selection. That signals |
| 59 | + // to React that the selections are conceptually equal, and we can bail |
| 60 | + // out of rendering. |
60 | 61 | if (isEqual(prevSelection, nextSelection)) {
|
| 62 | + // The snapshot still has changed, so make sure to update to not keep |
| 63 | + // old references alive |
61 | 64 | memoizedSnapshot = nextSnapshot
|
62 | 65 | return prevSelection
|
63 | 66 | }
|
|
0 commit comments