Files
webstudio/apps/builder/app/shared/form-utils/use-ids.ts
T
Bogdan Chadkin 3ea15c5739 refactor: replace radix/react-id with useId from react (#4660)
React has builtin useId, now we can get rid of radix package for it.
2024-12-27 22:07:09 +07:00

26 lines
854 B
TypeScript

import { useId, useRef } from "react";
export const useIds = <FieldName extends string>(
fieldNames: readonly FieldName[]
) => {
if (process.env.NODE_ENV !== "production") {
// It's OK to disable because process.env.NODE_ENV is not going to change
// eslint-disable-next-line react-hooks/rules-of-hooks
const fieldNamesLength = useRef<number>(fieldNames.length);
if (fieldNamesLength.current !== fieldNames.length) {
throw new Error("changing fieldNames length is not supported");
}
fieldNamesLength.current = fieldNames.length;
}
const result: Record<string, string> = {};
for (const fieldName of fieldNames) {
// It's OK to disable because of the check above
// eslint-disable-next-line react-hooks/rules-of-hooks
result[fieldName] = useId();
}
return result as Record<FieldName, string>;
};