Skip to content

Commit

Permalink
Add useId to react-debug-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
acdlite committed Oct 31, 2021
1 parent c95ff83 commit 2dd94d8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,14 @@ function useOpaqueIdentifier(): OpaqueIDType | void {
}

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

const Dispatcher: DispatcherType = {
Expand Down
Original file line number Diff line number Diff line change
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

0 comments on commit 2dd94d8

Please sign in to comment.