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

Function useValueId

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

Source from the content-addressed store, hash-verified

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

Callers 1

useComboBoxFunction · 0.85

Calls 1

useIdFunction · 0.90

Tested by

no test coverage detected