Skip to content
Merged

useId #22644

Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ function useOpaqueIdentifier(): OpaqueIDType | void {
return value;
}

function useId(): string {
const hook = nextHook();
const id = hook !== null ? hook.memoizedState : '';
hookLog.push({
primitive: 'Id',
stackError: new Error(),
value: id,
});
return id;
}

const Dispatcher: DispatcherType = {
getCacheForType,
readContext,
Expand All @@ -361,6 +372,7 @@ const Dispatcher: DispatcherType = {
useSyncExternalStore,
useDeferredValue,
useOpaqueIdentifier,
useId,
};

// Inspect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ describe('ReactHooksInspectionIntegration', () => {
it('should support composite useOpaqueIdentifier hook in concurrent mode', () => {
function Foo(props) {
const id = React.unstable_useOpaqueIdentifier();
const [state] = React.useState(() => 'hello', []);
const [state] = React.useState('hello');
return <div id={id}>{state}</div>;
}

Expand Down Expand Up @@ -656,6 +656,33 @@ describe('ReactHooksInspectionIntegration', () => {
});
});

it('should support useId hook', () => {
function Foo(props) {
const id = React.unstable_useId();
const [state] = React.useState('hello');
return <div id={id}>{state}</div>;
}

const renderer = ReactTestRenderer.create(<Foo />);
const childFiber = renderer.root.findByType(Foo)._currentFiber();
const tree = ReactDebugTools.inspectHooksOfFiber(childFiber);

expect(tree.length).toEqual(2);

expect(tree[0].id).toEqual(0);
expect(tree[0].isStateEditable).toEqual(false);
expect(tree[0].name).toEqual('Id');
expect(String(tree[0].value).startsWith('r:')).toBe(true);

expect(tree[1]).toEqual({
id: 1,
isStateEditable: true,
name: 'State',
value: 'hello',
subHooks: [],
});
});

describe('useDebugValue', () => {
it('should support inspectable values for multiple custom hooks', () => {
function useLabeledValue(label) {
Expand Down
Loading