/
githubmirror
/
material-ui
Обзор
Документация
Войти
/
githubmirror
/
material-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
packages/mui-utils/src/useId/useId.ts
44 строки
2 KB
Brijesh Bittu
[code-infra] Bump to latest code-infra packages (#48672)
15 июн 2026, 11:37
Не верифицирован
15 июн 2026, 11:37
8f8e4dd
Код
Авторство
О чём код?
'use client'; import * as React from 'react'; let globalId = 0; // TODO React 17: Remove `useGlobalId` once React 17 support is removed function useGlobalId(idOverride?: string): string | undefined { const [defaultId, setDefaultId] = React.useState(idOverride); const id = idOverride || defaultId; React.useEffect(() => { if (defaultId == null) { // Fallback to this default id when possible. // Use the incrementing value for client-side rendering only. // We can't use it server-side. // If you want to use random values please consider the Birthday Problem: https://en.wikipedia.org/wiki/Birthday_problem globalId += 1; // eslint-disable-next-line react-hooks/set-state-in-effect setDefaultId(`mui-${globalId}`); } }, [defaultId]); return id; } // See https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379 for why const safeReact = { ...React }; const maybeReactUseId: undefined | (() => string) = safeReact.useId; /** * * @example <div id={useId()} /> * @param idOverride * @returns {string} */ export default function useId(idOverride?: string): string | undefined { // React.useId() is only available from React 17.0.0. if (maybeReactUseId !== undefined) { const reactId = maybeReactUseId(); return idOverride ?? reactId; } // TODO: uncomment once we enable eslint-plugin-react-compiler // eslint-disable-next-line react-compiler/react-compiler // eslint-disable-next-line react-hooks/rules-of-hooks -- `React.useId` is invariant at runtime. return useGlobalId(idOverride); }