MCPcopy
hub / github.com/adobe/react-spectrum / useValueId

Function useValueId

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

Source from the content-addressed store, hash-verified

511// prior to the state being updated. This results in onSelectionChange being called multiple times.
512// TODO: refactor useComboBoxState to avoid this.
513function useValueId(depArray: ReadonlyArray<any> = []): string | undefined {
514 let id = useId();
515 let [exists, setExists] = useState(true);
516 let [lastDeps, setLastDeps] = useState(depArray);
517
518 // If the deps changed, set exists to true so we can test whether the element exists.
519 if (lastDeps.some((v, i) => !Object.is(v, depArray[i]))) {
520 setExists(true);
521 setLastDeps(depArray);
522 }
523
524 useEffect(() => {
525 if (exists && !document.getElementById(id)) {
526 // oxlint-disable-next-line react-hooks-js/set-state-in-effect
527 setExists(false);
528 }
529 }, [id, exists, lastDeps]);
530
531 return exists ? id : undefined;
532}

Callers 1

useComboBoxFunction · 0.85

Calls 1

useIdFunction · 0.90

Tested by

no test coverage detected