react-hook-form-generator
Version:
A [React](https://reactjs.org/) component to quickly and easily generate forms from object schema. Built with [React Hook Form](https://react-hook-form.com/) and [Chakra UI](https://chakra-ui.com/).
18 lines (12 loc) • 469 B
text/typescript
import { useMemo } from 'react';
import { useFormContext } from 'react-hook-form';
export const useErrorMessage = (name: string, label?: string) => {
const { errors } = useFormContext();
return useMemo(() => {
const error = errors[name];
if (!error) return undefined;
const message = error.message;
if (message) return message.replace(name, label || name);
return 'Field validation failed';
}, [errors, name, label]);
};