Skip to content

Commit 2dd94d8

Browse files
committed
Add useId to react-debug-tools
1 parent c95ff83 commit 2dd94d8

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Diff for: packages/react-debug-tools/src/ReactDebugHooks.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,14 @@ function useOpaqueIdentifier(): OpaqueIDType | void {
342342
}
343343

344344
function useId(): string {
345-
throw new Error('Not implemented.');
345+
const hook = nextHook();
346+
const id = hook !== null ? hook.memoizedState : '';
347+
hookLog.push({
348+
primitive: 'Id',
349+
stackError: new Error(),
350+
value: id,
351+
});
352+
return id;
346353
}
347354

348355
const Dispatcher: DispatcherType = {

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

+27
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,33 @@ describe('ReactHooksInspectionIntegration', () => {
656656
});
657657
});
658658

659+
it('should support useId hook', () => {
660+
function Foo(props) {
661+
const id = React.unstable_useId();
662+
const [state] = React.useState(() => 'hello', []);
663+
return <div id={id}>{state}</div>;
664+
}
665+
666+
const renderer = ReactTestRenderer.create(<Foo />);
667+
const childFiber = renderer.root.findByType(Foo)._currentFiber();
668+
const tree = ReactDebugTools.inspectHooksOfFiber(childFiber);
669+
670+
expect(tree.length).toEqual(2);
671+
672+
expect(tree[0].id).toEqual(0);
673+
expect(tree[0].isStateEditable).toEqual(false);
674+
expect(tree[0].name).toEqual('Id');
675+
expect(String(tree[0].value).startsWith('r:')).toBe(true);
676+
677+
expect(tree[1]).toEqual({
678+
id: 1,
679+
isStateEditable: true,
680+
name: 'State',
681+
value: 'hello',
682+
subHooks: [],
683+
});
684+
});
685+
659686
describe('useDebugValue', () => {
660687
it('should support inspectable values for multiple custom hooks', () => {
661688
function useLabeledValue(label) {

0 commit comments

Comments
 (0)