Skip to content

Commit 72ebc70

Browse files
authored
[DevTools] fix useDeferredValue to match reconciler change (#24742)
* [DevTools] fix useDeferredValue to match reconciler change * fixup * update test to catch original issue * fix lint * add safer tests for other composite hooks
1 parent a7c322c commit 72ebc70

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,11 @@ function useTransition(): [
310310
}
311311

312312
function useDeferredValue<T>(value: T): T {
313-
// useDeferredValue() composes multiple hooks internally.
314-
// Advance the current hook index the same number of times
315-
// so that subsequent hooks have the right memoized state.
316-
nextHook(); // State
317-
nextHook(); // Effect
313+
const hook = nextHook();
318314
hookLog.push({
319315
primitive: 'DeferredValue',
320316
stackError: new Error(),
321-
value,
317+
value: hook !== null ? hook.memoizedState : value,
322318
});
323319
return value;
324320
}

packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ describe('ReactHooksInspectionIntegration', () => {
546546
function Foo(props) {
547547
React.useTransition();
548548
const memoizedValue = React.useMemo(() => 'hello', []);
549+
React.useMemo(() => 'not used', []);
549550
return <div>{memoizedValue}</div>;
550551
}
551552
const renderer = ReactTestRenderer.create(<Foo />);
@@ -566,16 +567,24 @@ describe('ReactHooksInspectionIntegration', () => {
566567
value: 'hello',
567568
subHooks: [],
568569
},
570+
{
571+
id: 2,
572+
isStateEditable: false,
573+
name: 'Memo',
574+
value: 'not used',
575+
subHooks: [],
576+
},
569577
]);
570578
});
571579

572-
it('should support composite useDeferredValue hook', () => {
580+
it('should support useDeferredValue hook', () => {
573581
function Foo(props) {
574582
React.useDeferredValue('abc', {
575583
timeoutMs: 500,
576584
});
577-
const [state] = React.useState(() => 'hello', []);
578-
return <div>{state}</div>;
585+
const memoizedValue = React.useMemo(() => 1, []);
586+
React.useMemo(() => 2, []);
587+
return <div>{memoizedValue}</div>;
579588
}
580589
const renderer = ReactTestRenderer.create(<Foo />);
581590
const childFiber = renderer.root.findByType(Foo)._currentFiber();
@@ -590,9 +599,16 @@ describe('ReactHooksInspectionIntegration', () => {
590599
},
591600
{
592601
id: 1,
593-
isStateEditable: true,
594-
name: 'State',
595-
value: 'hello',
602+
isStateEditable: false,
603+
name: 'Memo',
604+
value: 1,
605+
subHooks: [],
606+
},
607+
{
608+
id: 2,
609+
isStateEditable: false,
610+
name: 'Memo',
611+
value: 2,
596612
subHooks: [],
597613
},
598614
]);
@@ -1012,6 +1028,7 @@ describe('ReactHooksInspectionIntegration', () => {
10121028
() => {},
10131029
);
10141030
React.useMemo(() => 'memo', []);
1031+
React.useMemo(() => 'not used', []);
10151032
return <div />;
10161033
}
10171034
const renderer = ReactTestRenderer.create(<Foo />);
@@ -1032,6 +1049,13 @@ describe('ReactHooksInspectionIntegration', () => {
10321049
value: 'memo',
10331050
subHooks: [],
10341051
},
1052+
{
1053+
id: 2,
1054+
isStateEditable: false,
1055+
name: 'Memo',
1056+
value: 'not used',
1057+
subHooks: [],
1058+
},
10351059
]);
10361060
});
10371061

@@ -1043,6 +1067,7 @@ describe('ReactHooksInspectionIntegration', () => {
10431067
() => 'snapshot',
10441068
);
10451069
React.useMemo(() => 'memo', []);
1070+
React.useMemo(() => 'not used', []);
10461071
return value;
10471072
}
10481073

@@ -1064,6 +1089,13 @@ describe('ReactHooksInspectionIntegration', () => {
10641089
value: 'memo',
10651090
subHooks: [],
10661091
},
1092+
{
1093+
id: 2,
1094+
isStateEditable: false,
1095+
name: 'Memo',
1096+
value: 'not used',
1097+
subHooks: [],
1098+
},
10671099
]);
10681100
});
10691101
});

0 commit comments

Comments
 (0)