admin管理员组文章数量:1024174
I'm trying to use react hook form with custom TextInput. Before I was using materials input and everything was working correctly. A found one way that I can achieve it but I'm not satisfied with that.
I'm using useForm
hook
const {
register,
handleSubmit,
formState: { errors, isValid },
} = useForm<FormValues>({ resolver: yupResolver(schema), mode: "all" });
And my custom TextField (pretty simple because for now I want just to register it into form):
export interface TextFieldProps {
id: string;
error: string | undefined;
label: string;
register: UseFormRegister<any>
}
const TextField = ({ id, error, label, register }: TextFieldProps): JSX.Element => {
return <>
<input {...register(`${id}`)}></input>
{error}
</>
};
using that ponent:
<TextField
register={register}
id="username"
label="Username"
error={errors.username?.message}
/>
This code is working but I'm losing IMO very nice feature - checking the name that I passed to register function. For example I have some schema declared:
const schema = yup.object({
username: yup.string().required("Username is required"),
password: yup.string().required("Password is required"),
});
And If I try the code below. Typescript will say me that I don't have email field in my schema.
<input
{...register("email")}>
</input>
I'm still trying to modify TextInput ponent to be able to use it like:
<TextField
{...register("username")}
id="username"
label="Username"
error={errors.username?.message}
/>
but I'm still facing with warning Function ponents cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
How should that TextField ponent look to avoid that forwardRef warning?
I'm trying to use react hook form with custom TextInput. Before I was using materials input and everything was working correctly. A found one way that I can achieve it but I'm not satisfied with that.
I'm using useForm
hook
const {
register,
handleSubmit,
formState: { errors, isValid },
} = useForm<FormValues>({ resolver: yupResolver(schema), mode: "all" });
And my custom TextField (pretty simple because for now I want just to register it into form):
export interface TextFieldProps {
id: string;
error: string | undefined;
label: string;
register: UseFormRegister<any>
}
const TextField = ({ id, error, label, register }: TextFieldProps): JSX.Element => {
return <>
<input {...register(`${id}`)}></input>
{error}
</>
};
using that ponent:
<TextField
register={register}
id="username"
label="Username"
error={errors.username?.message}
/>
This code is working but I'm losing IMO very nice feature - checking the name that I passed to register function. For example I have some schema declared:
const schema = yup.object({
username: yup.string().required("Username is required"),
password: yup.string().required("Password is required"),
});
And If I try the code below. Typescript will say me that I don't have email field in my schema.
<input
{...register("email")}>
</input>
I'm still trying to modify TextInput ponent to be able to use it like:
<TextField
{...register("username")}
id="username"
label="Username"
error={errors.username?.message}
/>
but I'm still facing with warning Function ponents cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
How should that TextField ponent look to avoid that forwardRef warning?
-
Why not pass
UseFormRegisterReturn
instead ofUseFormRegister
? This lets you callregister
from the outside and you get the type-safety. – Florian Walther Commented Mar 8, 2023 at 9:29 -
You don't need to pass
register
at all - it's passed inref
. See answer by @Joris. Should be accepted - works as a charm – lentyai Commented Oct 8, 2023 at 3:16
1 Answer
Reset to default 6You've to pass the ref
and below is one of the ways to achieve that
export interface TextFieldProps extends React.PropsWithoutRef<JSX.IntrinsicElements["input"]> {
error: string | undefined;
label: string;
}
const TextField = forwardRef<HTMLInputElement, LabeledTextFieldProps>({ errorm label, ...props }, ref) => {
return (
<>
<input {...props} ref={ref}></input>
{error}
</>
)
});
You're to be able to use it like:
<TextField {...register('username')} label="Username label" error={formState.errors.username.message}/>
I'm trying to use react hook form with custom TextInput. Before I was using materials input and everything was working correctly. A found one way that I can achieve it but I'm not satisfied with that.
I'm using useForm
hook
const {
register,
handleSubmit,
formState: { errors, isValid },
} = useForm<FormValues>({ resolver: yupResolver(schema), mode: "all" });
And my custom TextField (pretty simple because for now I want just to register it into form):
export interface TextFieldProps {
id: string;
error: string | undefined;
label: string;
register: UseFormRegister<any>
}
const TextField = ({ id, error, label, register }: TextFieldProps): JSX.Element => {
return <>
<input {...register(`${id}`)}></input>
{error}
</>
};
using that ponent:
<TextField
register={register}
id="username"
label="Username"
error={errors.username?.message}
/>
This code is working but I'm losing IMO very nice feature - checking the name that I passed to register function. For example I have some schema declared:
const schema = yup.object({
username: yup.string().required("Username is required"),
password: yup.string().required("Password is required"),
});
And If I try the code below. Typescript will say me that I don't have email field in my schema.
<input
{...register("email")}>
</input>
I'm still trying to modify TextInput ponent to be able to use it like:
<TextField
{...register("username")}
id="username"
label="Username"
error={errors.username?.message}
/>
but I'm still facing with warning Function ponents cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
How should that TextField ponent look to avoid that forwardRef warning?
I'm trying to use react hook form with custom TextInput. Before I was using materials input and everything was working correctly. A found one way that I can achieve it but I'm not satisfied with that.
I'm using useForm
hook
const {
register,
handleSubmit,
formState: { errors, isValid },
} = useForm<FormValues>({ resolver: yupResolver(schema), mode: "all" });
And my custom TextField (pretty simple because for now I want just to register it into form):
export interface TextFieldProps {
id: string;
error: string | undefined;
label: string;
register: UseFormRegister<any>
}
const TextField = ({ id, error, label, register }: TextFieldProps): JSX.Element => {
return <>
<input {...register(`${id}`)}></input>
{error}
</>
};
using that ponent:
<TextField
register={register}
id="username"
label="Username"
error={errors.username?.message}
/>
This code is working but I'm losing IMO very nice feature - checking the name that I passed to register function. For example I have some schema declared:
const schema = yup.object({
username: yup.string().required("Username is required"),
password: yup.string().required("Password is required"),
});
And If I try the code below. Typescript will say me that I don't have email field in my schema.
<input
{...register("email")}>
</input>
I'm still trying to modify TextInput ponent to be able to use it like:
<TextField
{...register("username")}
id="username"
label="Username"
error={errors.username?.message}
/>
but I'm still facing with warning Function ponents cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
How should that TextField ponent look to avoid that forwardRef warning?
-
Why not pass
UseFormRegisterReturn
instead ofUseFormRegister
? This lets you callregister
from the outside and you get the type-safety. – Florian Walther Commented Mar 8, 2023 at 9:29 -
You don't need to pass
register
at all - it's passed inref
. See answer by @Joris. Should be accepted - works as a charm – lentyai Commented Oct 8, 2023 at 3:16
1 Answer
Reset to default 6You've to pass the ref
and below is one of the ways to achieve that
export interface TextFieldProps extends React.PropsWithoutRef<JSX.IntrinsicElements["input"]> {
error: string | undefined;
label: string;
}
const TextField = forwardRef<HTMLInputElement, LabeledTextFieldProps>({ errorm label, ...props }, ref) => {
return (
<>
<input {...props} ref={ref}></input>
{error}
</>
)
});
You're to be able to use it like:
<TextField {...register('username')} label="Username label" error={formState.errors.username.message}/>
本文标签: javascriptuse react hook form with custom TextInputStack Overflow
版权声明:本文标题:javascript - use react hook form with custom TextInput - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745574599a2156941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论