![](/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.