GeezerVison is showing github.com — Fix useId in strict mode (#22681) · react/react@00ced1e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMUseId-test.js +57Lines changed: 57 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ let ReactDOMFizzServer; | 16 | 16 | let Stream; | 17 | 17 | let Suspense; | 18 | 18 | let useId; | | 19 | +let useState; | 19 | 20 | let document; | 20 | 21 | let writable; | 21 | 22 | let container; | @@ -35,6 +36,7 @@ describe('useId', () => { | 35 | 36 | Stream = require('stream'); | 36 | 37 | Suspense = React.Suspense; | 37 | 38 | useId = React.useId; | | 39 | + useState = React.useState; | 38 | 40 |
| 39 | 41 | // Test Environment | 40 | 42 | const jsdom = new JSDOM( | @@ -198,6 +200,35 @@ describe('useId', () => { | 198 | 200 | `); | 199 | 201 | }); | 200 | 202 |
| | 203 | + test('StrictMode double rendering', async () => { | | 204 | + const {StrictMode} = React; | | 205 | + | | 206 | + function App() { | | 207 | + return ( | | 208 | + <StrictMode> | | 209 | + <DivWithId /> | | 210 | + </StrictMode> | | 211 | + ); | | 212 | + } | | 213 | + | | 214 | + await serverAct(async () => { | | 215 | + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />); | | 216 | + pipe(writable); | | 217 | + }); | | 218 | + await clientAct(async () => { | | 219 | + ReactDOM.hydrateRoot(container, <App />); | | 220 | + }); | | 221 | + expect(container).toMatchInlineSnapshot(` | | 222 | + <div | | 223 | + id="container" | | 224 | + > | | 225 | + <div | | 226 | + id="0" | | 227 | + /> | | 228 | + </div> | | 229 | + `); | | 230 | + }); | | 231 | + | 201 | 232 | test('empty (null) children', async () => { | 202 | 233 | // We don't treat empty children different from non-empty ones, which means | 203 | 234 | // they get allocated a slot when generating ids. There's no inherent reason | @@ -313,6 +344,32 @@ describe('useId', () => { | 313 | 344 | `); | 314 | 345 | }); | 315 | 346 |
| | 347 | + test('local render phase updates', async () => { | | 348 | + function App({swap}) { | | 349 | + const [count, setCount] = useState(0); | | 350 | + if (count < 3) { | | 351 | + setCount(count + 1); | | 352 | + } | | 353 | + return useId(); | | 354 | + } | | 355 | + | | 356 | + await serverAct(async () => { | | 357 | + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />); | | 358 | + pipe(writable); | | 359 | + }); | | 360 | + await clientAct(async () => { | | 361 | + ReactDOM.hydrateRoot(container, <App />); | | 362 | + }); | | 363 | + expect(container).toMatchInlineSnapshot(` | | 364 | + <div | | 365 | + id="container" | | 366 | + > | | 367 | + R:0 | | 368 | + <!-- --> | | 369 | + </div> | | 370 | + `); | | 371 | + }); | | 372 | + | 316 | 373 | test('basic incremental hydration', async () => { | 317 | 374 | function App() { | 318 | 375 | return ( | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberBeginWork.new.js +30-1Lines changed: 30 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -174,7 +174,11 @@ import { | 174 | 174 | prepareToReadContext, | 175 | 175 | scheduleWorkOnParentPath, | 176 | 176 | } from './ReactFiberNewContext.new'; | 177 | | -import {renderWithHooks, bailoutHooks} from './ReactFiberHooks.new'; | | 177 | +import { | | 178 | + renderWithHooks, | | 179 | + checkDidRenderIdHook, | | 180 | + bailoutHooks, | | 181 | +} from './ReactFiberHooks.new'; | 178 | 182 | import {stopProfilerTimerIfRunning} from './ReactProfilerTimer.new'; | 179 | 183 | import { | 180 | 184 | getMaskedContext, | @@ -240,6 +244,7 @@ import { | 240 | 244 | getForksAtLevel, | 241 | 245 | isForkedChild, | 242 | 246 | pushTreeId, | | 247 | + pushMaterializedTreeId, | 243 | 248 | } from './ReactFiberTreeContext.new'; | 244 | 249 |
| 245 | 250 | const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner; | @@ -365,6 +370,7 @@ function updateForwardRef( | 365 | 370 |
| 366 | 371 | // The rest is a fork of updateFunctionComponent | 367 | 372 | let nextChildren; | | 373 | + let hasId; | 368 | 374 | prepareToReadContext(workInProgress, renderLanes); | 369 | 375 | if (enableSchedulingProfiler) { | 370 | 376 | markComponentRenderStarted(workInProgress); | @@ -380,6 +386,7 @@ function updateForwardRef( | 380 | 386 | ref, | 381 | 387 | renderLanes, | 382 | 388 | ); | | 389 | + hasId = checkDidRenderIdHook(); | 383 | 390 | if ( | 384 | 391 | debugRenderPhaseSideEffectsForStrictMode && | 385 | 392 | workInProgress.mode & StrictLegacyMode | @@ -394,6 +401,7 @@ function updateForwardRef( | 394 | 401 | ref, | 395 | 402 | renderLanes, | 396 | 403 | ); | | 404 | + hasId = checkDidRenderIdHook(); | 397 | 405 | } finally { | 398 | 406 | setIsStrictModeForDevtools(false); | 399 | 407 | } | @@ -408,6 +416,7 @@ function updateForwardRef( | 408 | 416 | ref, | 409 | 417 | renderLanes, | 410 | 418 | ); | | 419 | + hasId = checkDidRenderIdHook(); | 411 | 420 | } | 412 | 421 | if (enableSchedulingProfiler) { | 413 | 422 | markComponentRenderStopped(); | @@ -418,6 +427,10 @@ function updateForwardRef( | 418 | 427 | return bailoutOnAlreadyFinishedWork(current, workInProgress, renderLanes); | 419 | 428 | } | 420 | 429 |
| | 430 | + if (getIsHydrating() && hasId) { | | 431 | + pushMaterializedTreeId(workInProgress); | | 432 | + } | | 433 | + | 421 | 434 | // React DevTools reads this flag. | 422 | 435 | workInProgress.flags |= PerformedWork; | 423 | 436 | reconcileChildren(current, workInProgress, nextChildren, renderLanes); | @@ -970,6 +983,7 @@ function updateFunctionComponent( | 970 | 983 | } | 971 | 984 |
| 972 | 985 | let nextChildren; | | 986 | + let hasId; | 973 | 987 | prepareToReadContext(workInProgress, renderLanes); | 974 | 988 | if (enableSchedulingProfiler) { | 975 | 989 | markComponentRenderStarted(workInProgress); | @@ -985,6 +999,7 @@ function updateFunctionComponent( | 985 | 999 | context, | 986 | 1000 | renderLanes, | 987 | 1001 | ); | | 1002 | + hasId = checkDidRenderIdHook(); | 988 | 1003 | if ( | 989 | 1004 | debugRenderPhaseSideEffectsForStrictMode && | 990 | 1005 | workInProgress.mode & StrictLegacyMode | @@ -999,6 +1014,7 @@ function updateFunctionComponent( | 999 | 1014 | context, | 1000 | 1015 | renderLanes, | 1001 | 1016 | ); | | 1017 | + hasId = checkDidRenderIdHook(); | 1002 | 1018 | } finally { | 1003 | 1019 | setIsStrictModeForDevtools(false); | 1004 | 1020 | } | @@ -1013,6 +1029,7 @@ function updateFunctionComponent( | 1013 | 1029 | context, | 1014 | 1030 | renderLanes, | 1015 | 1031 | ); | | 1032 | + hasId = checkDidRenderIdHook(); | 1016 | 1033 | } | 1017 | 1034 | if (enableSchedulingProfiler) { | 1018 | 1035 | markComponentRenderStopped(); | @@ -1023,6 +1040,10 @@ function updateFunctionComponent( | 1023 | 1040 | return bailoutOnAlreadyFinishedWork(current, workInProgress, renderLanes); | 1024 | 1041 | } | 1025 | 1042 |
| | 1043 | + if (getIsHydrating() && hasId) { | | 1044 | + pushMaterializedTreeId(workInProgress); | | 1045 | + } | | 1046 | + | 1026 | 1047 | // React DevTools reads this flag. | 1027 | 1048 | workInProgress.flags |= PerformedWork; | 1028 | 1049 | reconcileChildren(current, workInProgress, nextChildren, renderLanes); | @@ -1593,6 +1614,7 @@ function mountIndeterminateComponent( | 1593 | 1614 |
| 1594 | 1615 | prepareToReadContext(workInProgress, renderLanes); | 1595 | 1616 | let value; | | 1617 | + let hasId; | 1596 | 1618 |
| 1597 | 1619 | if (enableSchedulingProfiler) { | 1598 | 1620 | markComponentRenderStarted(workInProgress); | @@ -1629,6 +1651,7 @@ function mountIndeterminateComponent( | 1629 | 1651 | context, | 1630 | 1652 | renderLanes, | 1631 | 1653 | ); | | 1654 | + hasId = checkDidRenderIdHook(); | 1632 | 1655 | setIsRendering(false); | 1633 | 1656 | } else { | 1634 | 1657 | value = renderWithHooks( | @@ -1639,6 +1662,7 @@ function mountIndeterminateComponent( | 1639 | 1662 | context, | 1640 | 1663 | renderLanes, | 1641 | 1664 | ); | | 1665 | + hasId = checkDidRenderIdHook(); | 1642 | 1666 | } | 1643 | 1667 | if (enableSchedulingProfiler) { | 1644 | 1668 | markComponentRenderStopped(); | @@ -1758,12 +1782,17 @@ function mountIndeterminateComponent( | 1758 | 1782 | context, | 1759 | 1783 | renderLanes, | 1760 | 1784 | ); | | 1785 | + hasId = checkDidRenderIdHook(); | 1761 | 1786 | } finally { | 1762 | 1787 | setIsStrictModeForDevtools(false); | 1763 | 1788 | } | 1764 | 1789 | } | 1765 | 1790 | } | 1766 | 1791 |
| | 1792 | + if (getIsHydrating() && hasId) { | | 1793 | + pushMaterializedTreeId(workInProgress); | | 1794 | + } | | 1795 | + | 1767 | 1796 | reconcileChildren(null, workInProgress, value, renderLanes); | 1768 | 1797 | if (__DEV__) { | 1769 | 1798 | validateFunctionComponentInDev(workInProgress, Component); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberBeginWork.old.js +30-1Lines changed: 30 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -174,7 +174,11 @@ import { | 174 | 174 | prepareToReadContext, | 175 | 175 | scheduleWorkOnParentPath, | 176 | 176 | } from './ReactFiberNewContext.old'; | 177 | | -import {renderWithHooks, bailoutHooks} from './ReactFiberHooks.old'; | | 177 | +import { | | 178 | + renderWithHooks, | | 179 | + checkDidRenderIdHook, | | 180 | + bailoutHooks, | | 181 | +} from './ReactFiberHooks.old'; | 178 | 182 | import {stopProfilerTimerIfRunning} from './ReactProfilerTimer.old'; | 179 | 183 | import { | 180 | 184 | getMaskedContext, | @@ -240,6 +244,7 @@ import { | 240 | 244 | getForksAtLevel, | 241 | 245 | isForkedChild, | 242 | 246 | pushTreeId, | | 247 | + pushMaterializedTreeId, | 243 | 248 | } from './ReactFiberTreeContext.old'; | 244 | 249 |
| 245 | 250 | const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner; | @@ -365,6 +370,7 @@ function updateForwardRef( | 365 | 370 |
| 366 | 371 | // The rest is a fork of updateFunctionComponent | 367 | 372 | let nextChildren; | | 373 | + let hasId; | 368 | 374 | prepareToReadContext(workInProgress, renderLanes); | 369 | 375 | if (enableSchedulingProfiler) { | 370 | 376 | markComponentRenderStarted(workInProgress); | @@ -380,6 +386,7 @@ function updateForwardRef( | 380 | 386 | ref, | 381 | 387 | renderLanes, | 382 | 388 | ); | | 389 | + hasId = checkDidRenderIdHook(); | 383 | 390 | if ( | 384 | 391 | debugRenderPhaseSideEffectsForStrictMode && | 385 | 392 | workInProgress.mode & StrictLegacyMode | @@ -394,6 +401,7 @@ function updateForwardRef( | 394 | 401 | ref, | 395 | 402 | renderLanes, | 396 | 403 | ); | | 404 | + hasId = checkDidRenderIdHook(); | 397 | 405 | } finally { | 398 | 406 | setIsStrictModeForDevtools(false); | 399 | 407 | } | @@ -408,6 +416,7 @@ function updateForwardRef( | 408 | 416 | ref, | 409 | 417 | renderLanes, | 410 | 418 | ); | | 419 | + hasId = checkDidRenderIdHook(); | 411 | 420 | } | 412 | 421 | if (enableSchedulingProfiler) { | 413 | 422 | markComponentRenderStopped(); | @@ -418,6 +427,10 @@ function updateForwardRef( | 418 | 427 | return bailoutOnAlreadyFinishedWork(current, workInProgress, renderLanes); | 419 | 428 | } | 420 | 429 |
| | 430 | + if (getIsHydrating() && hasId) { | | 431 | + pushMaterializedTreeId(workInProgress); | | 432 | + } | | 433 | + | 421 | 434 | // React DevTools reads this flag. | 422 | 435 | workInProgress.flags |= PerformedWork; | 423 | 436 | reconcileChildren(current, workInProgress, nextChildren, renderLanes); | @@ -970,6 +983,7 @@ function updateFunctionComponent( | 970 | 983 | } | 971 | 984 |
| 972 | 985 | let nextChildren; | | 986 | + let hasId; | 973 | 987 | prepareToReadContext(workInProgress, renderLanes); | 974 | 988 | if (enableSchedulingProfiler) { | 975 | 989 | markComponentRenderStarted(workInProgress); | @@ -985,6 +999,7 @@ function updateFunctionComponent( | 985 | 999 | context, | 986 | 1000 | renderLanes, | 987 | 1001 | ); | | 1002 | + hasId = checkDidRenderIdHook(); | 988 | 1003 | if ( | 989 | 1004 | debugRenderPhaseSideEffectsForStrictMode && | 990 | 1005 | workInProgress.mode & StrictLegacyMode | @@ -999,6 +1014,7 @@ function updateFunctionComponent( | 999 | 1014 | context, | 1000 | 1015 | renderLanes, | 1001 | 1016 | ); | | 1017 | + hasId = checkDidRenderIdHook(); | 1002 | 1018 | } finally { | 1003 | 1019 | setIsStrictModeForDevtools(false); | 1004 | 1020 | } | @@ -1013,6 +1029,7 @@ function updateFunctionComponent( | 1013 | 1029 | context, | 1014 | 1030 | renderLanes, | 1015 | 1031 | ); | | 1032 | + hasId = checkDidRenderIdHook(); | 1016 | 1033 | } | 1017 | 1034 | if (enableSchedulingProfiler) { | 1018 | 1035 | markComponentRenderStopped(); | @@ -1023,6 +1040,10 @@ function updateFunctionComponent( | 1023 | 1040 | return bailoutOnAlreadyFinishedWork(current, workInProgress, renderLanes); | 1024 | 1041 | } | 1025 | 1042 |
| | 1043 | + if (getIsHydrating() && hasId) { | | 1044 | + pushMaterializedTreeId(workInProgress); | | 1045 | + } | | 1046 | + | 1026 | 1047 | // React DevTools reads this flag. | 1027 | 1048 | workInProgress.flags |= PerformedWork; | 1028 | 1049 | reconcileChildren(current, workInProgress, nextChildren, renderLanes); | @@ -1593,6 +1614,7 @@ function mountIndeterminateComponent( | 1593 | 1614 |
| 1594 | 1615 | prepareToReadContext(workInProgress, renderLanes); | 1595 | 1616 | let value; | | 1617 | + let hasId; | 1596 | 1618 |
| 1597 | 1619 | if (enableSchedulingProfiler) { | 1598 | 1620 | markComponentRenderStarted(workInProgress); | @@ -1629,6 +1651,7 @@ function mountIndeterminateComponent( | 1629 | 1651 | context, | 1630 | 1652 | renderLanes, | 1631 | 1653 | ); | | 1654 | + hasId = checkDidRenderIdHook(); | 1632 | 1655 | setIsRendering(false); | 1633 | 1656 | } else { | 1634 | 1657 | value = renderWithHooks( | @@ -1639,6 +1662,7 @@ function mountIndeterminateComponent( | 1639 | 1662 | context, | 1640 | 1663 | renderLanes, | 1641 | 1664 | ); | | 1665 | + hasId = checkDidRenderIdHook(); | 1642 | 1666 | } | 1643 | 1667 | if (enableSchedulingProfiler) { | 1644 | 1668 | markComponentRenderStopped(); | @@ -1758,12 +1782,17 @@ function mountIndeterminateComponent( | 1758 | 1782 | context, | 1759 | 1783 | renderLanes, | 1760 | 1784 | ); | | 1785 | + hasId = checkDidRenderIdHook(); | 1761 | 1786 | } finally { | 1762 | 1787 | setIsStrictModeForDevtools(false); | 1763 | 1788 | } | 1764 | 1789 | } | 1765 | 1790 | } | 1766 | 1791 |
| | 1792 | + if (getIsHydrating() && hasId) { | | 1793 | + pushMaterializedTreeId(workInProgress); | | 1794 | + } | | 1795 | + | 1767 | 1796 | reconcileChildren(null, workInProgress, value, renderLanes); | 1768 | 1797 | if (__DEV__) { | 1769 | 1798 | validateFunctionComponentInDev(workInProgress, Component); | |
|