MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useValueId

Function useValueId

packages/react-aria/src/combobox/useComboBox.ts:567–586  ·  view source on GitHub ↗
(depArray: ReadonlyArray<any> = [])

Source from the content-addressed store, hash-verified

565// prior to the state being updated. This results in onSelectionChange being called multiple times.
566// TODO: refactor useComboBoxState to avoid this.
567function useValueId(depArray: ReadonlyArray<any> = []): string | undefined {
568 let id = useId();
569 let [exists, setExists] = useState(true);
570 let [lastDeps, setLastDeps] = useState(depArray);
571
572 // If the deps changed, set exists to true so we can test whether the element exists.
573 if (lastDeps.some((v, i) => !Object.is(v, depArray[i]))) {
574 setExists(true);
575 setLastDeps(depArray);
576 }
577
578 useEffect(() => {
579 if (exists && !document.getElementById(id)) {
580 // oxlint-disable-next-line react/react-compiler
581 setExists(false);
582 }
583 }, [id, exists, lastDeps]);
584
585 return exists ? id : undefined;
586}

Callers 1

useComboBoxFunction · 0.85

Calls 1

useIdFunction · 0.90

Tested by

no test coverage detected