Skip to content

Commit a642c12

Browse files
author
Brian Vaughn
committed
Update ReactDebugHooks to handle composite hooks
1 parent 60016c4 commit a642c12

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,11 @@ function useResponder(
243243
function useTransition(
244244
config: SuspenseConfig | null | void,
245245
): [(() => void) => void, boolean] {
246-
nextHook();
246+
// useTransition() composes multiple hooks internally.
247+
// Advance the current hook index the same number of times
248+
// so that subsequent hooks have the right memoized state.
249+
nextHook(); // State
250+
nextHook(); // Callback
247251
hookLog.push({
248252
primitive: 'Transition',
249253
stackError: new Error(),
@@ -253,7 +257,11 @@ function useTransition(
253257
}
254258

255259
function useDeferredValue<T>(value: T, config: TimeoutConfig | null | void): T {
256-
nextHook();
260+
// useDeferredValue() composes multiple hooks internally.
261+
// Advance the current hook index the same number of times
262+
// so that subsequent hooks have the right memoized state.
263+
nextHook(); // State
264+
nextHook(); // Effect
257265
hookLog.push({
258266
primitive: 'DeferredValue',
259267
stackError: new Error(),

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,62 @@ describe('ReactHooksInspectionIntegration', () => {
366366
]);
367367
});
368368

369+
it('should support composite useTransition hook', () => {
370+
function Foo(props) {
371+
React.useTransition();
372+
const memoizedValue = React.useMemo(() => 'hello', []);
373+
return <div>{memoizedValue}</div>;
374+
}
375+
let renderer = ReactTestRenderer.create(<Foo />);
376+
let childFiber = renderer.root.findByType(Foo)._currentFiber();
377+
let tree = ReactDebugTools.inspectHooksOfFiber(childFiber);
378+
expect(tree).toEqual([
379+
{
380+
id: 0,
381+
isStateEditable: false,
382+
name: 'Transition',
383+
value: undefined,
384+
subHooks: [],
385+
},
386+
{
387+
id: 1,
388+
isStateEditable: false,
389+
name: 'Memo',
390+
value: 'hello',
391+
subHooks: [],
392+
},
393+
]);
394+
});
395+
396+
it('should support composite useDeferredValue hook', () => {
397+
function Foo(props) {
398+
React.useDeferredValue('abc', {
399+
timeoutMs: 500,
400+
});
401+
const [state] = React.useState(() => 'hello', []);
402+
return <div>{state}</div>;
403+
}
404+
let renderer = ReactTestRenderer.create(<Foo />);
405+
let childFiber = renderer.root.findByType(Foo)._currentFiber();
406+
let tree = ReactDebugTools.inspectHooksOfFiber(childFiber);
407+
expect(tree).toEqual([
408+
{
409+
id: 0,
410+
isStateEditable: false,
411+
name: 'DeferredValue',
412+
value: 'abc',
413+
subHooks: [],
414+
},
415+
{
416+
id: 1,
417+
isStateEditable: true,
418+
name: 'State',
419+
value: 'hello',
420+
subHooks: [],
421+
},
422+
]);
423+
});
424+
369425
describe('useDebugValue', () => {
370426
it('should support inspectable values for multiple custom hooks', () => {
371427
function useLabeledValue(label) {

0 commit comments

Comments
 (0)