[英]How to use react-hook-form with props and material UI
I'm using Material UI Select as a reusable component, and I want to validate it with react-hook-form by passing props from parent to child component.我正在使用 Material UI Select 作为可重用组件,我想通过从父组件传递道具到子组件来使用 react-hook-form 验证它。 So far, I've tried to use from RHF, and to pass some props to the child, but somehow error wont disappear when I select option.到目前为止,我已经尝试从 RHF 使用,并将一些道具传递给孩子,但是当我选择 select 时,错误不会消失。 This is my code这是我的代码
import React from 'react'; import styled from '@emotion/styled'; import { Select, MenuItem } from '@material-ui/core'; import { ASSelect } from '../../ASSelect'; import { Controller, useForm } from 'react-hook-form'; const { register, handleSubmit, watch, errors, control, setValue } = useForm(); const onSubmit = (data: any) => { console.log(errors); }; const defaultDashboard = [ { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' }, ]; const Parent = () => { return ( <Controller as={ <ASSelect menuItems={defaultDashboard} label="Default dashboard*" handleChange={dashboardHandler} value={dashboardValue} } name="Select" control={control} rules={{ required: true }} onChange={([selected]) => { return { value: selected }; }} /> {errors.Select? <span>Default dashboard is required</span>: null} ) } export {Parent}; const ASSelect = ({menuItems, label, handleChange, value}) => { return ( <div> <Select> {menuItems.map((el, index)=> { return <MenuItem key={index}>{el.label}</MenuItem> }} </Select> </div> )} export {ASSelect};
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
So when I submit a form without selecting anything, the error will popup.因此,当我在未选择任何内容的情况下提交表单时,会弹出错误。 But when I select an option, the error will remain there.但是当我选择 select 时,错误会一直存在。 What am I doing wrong?我究竟做错了什么?
You can use Select with Controller
from react-hook-forms in the following way:您可以通过以下方式将来自 react-hook-forms 的 Select 与Controller
一起使用:
const SelectController = ({ name, menuItems, control }) => (
<Controller
as={
<Select>
{menuItems.map(({ value, label }, index) => (
<MenuItem key={index} value={value}>
{label}
</MenuItem>
))}
}
</Select>
}
name={name}
control={control}
defaultValue=""
rules={{ required: true }}
/>
);
or use Select with setValue
from react-hook-forms as follows:或使用 Select 和来自 react-hook-forms 的setValue
如下:
const SelectSetValue = ({ name, menuItems, setValue }) => {
return (
<Select
name={name}
defaultValue=""
onChange={e => setValue(name, e.target.value)}
>
{menuItems.map(({ value, label }, index) => (
<MenuItem key={index} value={value}>
{label}
</MenuItem>
))}
}
</Select>
);
};
In both cases you get error-validation initially onSubmit event.在这两种情况下,您最初都会在 onSubmit 事件中获得错误验证。 Error display is updated afterwards for SelectController
when you select a value and for SelectSetValue
when you select a value and re-trigger onSubmit.当您 select 一个值和SelectSetValue
当您 select 一个值并重新触发 onSubmit 时,将更新SelectController
的错误显示。
You can check a working example at following the link:您可以通过以下链接查看工作示例:
https://codesandbox.io/s/somaterialselecthookform-kdext?file=/src/App.js https://codesandbox.io/s/somaterialselecthookform-kdext?file=/src/App.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.