| 555 | // prior to the state being updated. This results in onSelectionChange being called multiple times. |
| 556 | // TODO: refactor useComboBoxState to avoid this. |
| 557 | function 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 | } |