Skip to content

Commit ebf9ae8

Browse files
authored
useId (#22644)
* Add useId to dispatcher * Initial useId implementation Ids are base 32 strings whose binary representation corresponds to the position of a node in a tree. Every time the tree forks into multiple children, we add additional bits to the left of the sequence that represent the position of the child within the current level of children. 00101 00010001011010101 ╰─┬─╯ ╰───────┬───────╯ Fork 5 of 20 Parent id The leading 0s are important. In the above example, you only need 3 bits to represent slot 5. However, you need 5 bits to represent all the forks at the current level, so we must account for the empty bits at the end. For this same reason, slots are 1-indexed instead of 0-indexed. Otherwise, the zeroth id at a level would be indistinguishable from its parent. If a node has only one child, and does not materialize an id (i.e. does not contain a useId hook), then we don't need to allocate any space in the sequence. It's treated as a transparent indirection. For example, these two trees produce the same ids: <> <> <Indirection> <A /> <A /> <B /> </Indirection> </> <B /> </> However, we cannot skip any materializes an id. Otherwise, a parent id that does not fork would be indistinguishable from its child id. For example, this tree does not fork, but the parent and child must have different ids. <Parent> <Child /> </Parent> To handle this scenario, every time we materialize an id, we allocate a new level with a single slot. You can think of this as a fork with only one prong, or an array of children with length 1. It's possible for the the size of the sequence to exceed 32 bits, the max size for bitwise operations. When this happens, we make more room by converting the right part of the id to a string and storing it in an overflow variable. We use a base 32 string representation, because 32 is the largest power of 2 that is supported by toString(). We want the base to be large so that the resulting ids are compact, and we want the base to be a power of 2 because every log2(base) bits corresponds to a single character, i.e. every log2(32) = 5 bits. That means we can lop bits off the end 5 at a time without affecting the final result. * Incremental hydration Stores the tree context on the dehydrated Suspense boundary's state object so it resume where it left off. * Add useId to react-debug-tools * Add selective hydration test Demonstrates that selective hydration works and ids are preserved even after subsequent client updates.
1 parent a0d991f commit ebf9ae8

38 files changed

+1819
-77
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ function useOpaqueIdentifier(): OpaqueIDType | void {
341341
return value;
342342
}
343343

344+
function useId(): string {
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;
353+
}
354+
344355
const Dispatcher: DispatcherType = {
345356
getCacheForType,
346357
readContext,
@@ -361,6 +372,7 @@ const Dispatcher: DispatcherType = {
361372
useSyncExternalStore,
362373
useDeferredValue,
363374
useOpaqueIdentifier,
375+
useId,
364376
};
365377

366378
// Inspect

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

+28-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ describe('ReactHooksInspectionIntegration', () => {
628628
it('should support composite useOpaqueIdentifier hook in concurrent mode', () => {
629629
function Foo(props) {
630630
const id = React.unstable_useOpaqueIdentifier();
631-
const [state] = React.useState(() => 'hello', []);
631+
const [state] = React.useState('hello');
632632
return <div id={id}>{state}</div>;
633633
}
634634

@@ -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)