A functional component is implemented like this :
const TestComponent = () => {
const id1 = React.useId();
const id2 = React.useId();
const id3 = React.useId();
return (
<div>{ JSON.stringify([ id1, id2, id2 ]) }</div>
);
}
display this
[ ":Rkm:", ":RkmH1:", ":RkmH2:" ]
These ids seems not valid, why does React return these values?
** EDIT **
According to the official documentation, React.useId()
is used to assign id
HTML attributes :
function Checkbox() {
const id = useId();
return (
<>
<label htmlFor={id}>Do you like React?</label>
<input id={id} type="checkbox" name="react"/>
</>
);
};
And, according to the HTML5 specifications, id
attributes simply "must contain at least one character" and "must not contain any ASCII whitespace." However, React's documentation also states :
Note
useId
generates a string that includes the:
token. This helps ensure that the token is unique, but is not supported in CSS selectors or APIs likequerySelectorAll
.
So, why returning values that aren't compatible with querySelectorAll
?