-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useId #22644
useId #22644
Conversation
Comparing: a0d991f...371302f Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
75a0cc2
to
78df833
Compare
0e5c259
to
3fbc388
Compare
const divs = container.querySelectorAll('div'); | ||
|
||
// Confirm that every id matches the expected pattern | ||
for (let i = 0; i < divs.length; i++) { | ||
// Example: R:clalalalalalalala... | ||
expect(divs[i].id).toMatch(/^R:.(((al)*a?)((la)*l?))*$/); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For small trees collision is implicitly tested with a snapshot. But testing for collision is probably more interesting for large trees anyway. Does this make sense?
const divs = container.querySelectorAll('div'); | |
// Confirm that every id matches the expected pattern | |
for (let i = 0; i < divs.length; i++) { | |
// Example: R:clalalalalalalala... | |
expect(divs[i].id).toMatch(/^R:.(((al)*a?)((la)*l?))*$/); | |
const divs = container.querySelectorAll('div'); | |
const ids = new Set(); | |
// Confirm that every id matches the expected pattern and is unique | |
for (let i = 0; i < divs.length; i++) { | |
// Example: R:clalalalalalalala... | |
expect(divs[i].id).toMatch(/^R:.(((al)*a?)((la)*l?))*$/); | |
expect(ids).not.toContain(divs[i].id); | |
ids.add(divs[i].id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this particular test it’s probably not that interesting because the structure of the tree follows such a rigid pattern. I’m planning to do this with a fuzz tester like we do for other features like context and Suspense.
@@ -656,6 +656,33 @@ describe('ReactHooksInspectionIntegration', () => { | |||
}); | |||
}); | |||
|
|||
it('should support useOpaqueIdentifier hook', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: title
// <B /> | ||
// </> | ||
// | ||
// However, we cannot skip any materializes an id. Otherwise, a parent id that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: grammar
it('should support useId hook', () => { | ||
function Foo(props) { | ||
const id = React.unstable_useId(); | ||
const [state] = React.useState(() => 'hello', []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does React.useState
support a second argument? What does this stand for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No this was just copy pasta haha
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.
Stores the tree context on the dehydrated Suspense boundary's state object so it resume where it left off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome
'B', | ||
]); | ||
// The insertions should not cause a mismatch. | ||
expect(container).toMatchInlineSnapshot(` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also test deletions? I'm guessing we won't match if a divWithID is missing on the client? Maybe we should encode it in the html?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I can add a test for deleting a sibling. Or do you mean deleting the Suspense boundary before it hydrates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed out-of-band, added more tests
Demonstrates that selective hydration works and ids are preserved even after subsequent client updates.
Partial renderer (legacy server renderer) support? Do we need this?[I think I'll punt on this for now, since in open source the legacy API is implemented on top of the new renderer.]New hook API that generates stable ids during server rendering and hydration to avoid mismatches.
Outside of server-rendered content, it falls back to a global counter.
This is intended to replace
useOpaqueIdentifier
.Id generation algorithm
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.
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:
However, we cannot skip any node that 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.
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 = 1 base 32 character. That means we can lop bits off the end 5 at a time without affecting the final result.