GeezerVison is showing github.com — Add warning and test for useSyncExternalStore when getSnapshot isn't … · react/react@031abd2
File tree
Expand file treeCollapse file tree
Open diff view settings
packages/use-sync-external-store/src
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -618,4 +618,30 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => { |
618 | 618 | expect(root).toMatchRenderedOutput('A1B1'); |
619 | 619 | }); |
620 | 620 | }); |
| 621 | + |
| 622 | + test('Infinite loop if getSnapshot keeps returning new reference', () => { |
| 623 | + const store = createExternalStore({}); |
| 624 | + |
| 625 | + function App() { |
| 626 | + const text = useSyncExternalStore(store.subscribe, () => ({})); |
| 627 | + return <Text text={JSON.stringify(text)} />; |
| 628 | + } |
| 629 | + |
| 630 | + spyOnDev(console, 'error'); |
| 631 | + |
| 632 | + expect(() => { |
| 633 | + act(() => { |
| 634 | + createRoot(<App />); |
| 635 | + }); |
| 636 | + }).toThrow( |
| 637 | + 'Maximum update depth exceeded. This can happen when a component repeatedly ' + |
| 638 | + 'calls setState inside componentWillUpdate or componentDidUpdate. React limits ' + |
| 639 | + 'the number of nested updates to prevent infinite loops.', |
| 640 | + ); |
| 641 | + if (__DEV__) { |
| 642 | + expect(console.error.calls.argsFor(0)[0]).toMatch( |
| 643 | + 'The result of getSnapshot should be cached to avoid an infinite loop', |
| 644 | + ); |
| 645 | + } |
| 646 | + }); |
621 | 647 | }); |
Collapse file
Copy file name to clipboardExpand all lines: packages/use-sync-external-store/src/useSyncExternalStore.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ export const useSyncExternalStore = |
29 | 29 | builtInAPI !== undefined ? builtInAPI : useSyncExternalStore_shim; |
30 | 30 |
|
31 | 31 | let didWarnOld18Alpha = false; |
| 32 | +let didWarnUncachedGetSnapshot = false; |
32 | 33 |
|
33 | 34 | // Disclaimer: This shim breaks many of the rules of React, and only works |
34 | 35 | // because of a very particular set of implementation details and assumptions |
@@ -63,6 +64,16 @@ function useSyncExternalStore_shim<T>( |
63 | 64 | // implementation details, most importantly that updates are |
64 | 65 | // always synchronous. |
65 | 66 | const value = getSnapshot(); |
| 67 | + if (__DEV__) { |
| 68 | + if (!didWarnUncachedGetSnapshot) { |
| 69 | + if (value !== getSnapshot()) { |
| 70 | + console.error( |
| 71 | + 'The result of getSnapshot should be cached to avoid an infinite loop', |
| 72 | + ); |
| 73 | + didWarnUncachedGetSnapshot = true; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
66 | 77 |
|
67 | 78 | // Because updates are synchronous, we don't queue them. Instead we force a |
68 | 79 | // re-render whenever the subscribed state changes by updating an some |
|