![](/img/trans.png)
[英]react-select wrapped with react-hook-form Controller TS compiler error?
[英]React-hook-form with react-select formState not valid but controller don't giving an error
我使用 react-hook-form 进行验证。 如果 formState.isValid 为 false,则禁用提交按钮。 如果我不 select 选项 formState.isValid 返回 false,它就可以工作。 但是 controller 没有给出错误。 我怎样才能让它给出错误消息? 这是 controller 代码
const { register, errors, formState, control } = useForm({
mode: "all"
});
function changeValue(inputValue) {
onChange({ key: "expiryMonth", value: inputValue.value });
}
...
{errors.expiryMonth && <span className="input__error-message">required.</span>}
<Controller
name="expiryMonth"
defaultValue={null}
control={control}
value={expiryMonth}
render={({ onChange }) => <Select isSearchable={false} placeholder="Month" onChange={(inputValue) =>
onChange({ value: changeValue(inputValue) })} options={months} />}
rules={{ required: true }}
register={register}
/>
当您使用Controller
时,它会为您register
:
<Controller
render={(props) =>
<ReactSelect {...props}
options={[
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry" },
{ value: "vanilla", label: "Vanilla" }
]}
/>}
name="ReactSelect"
control={control}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.