material-ui - 带有 react-hook-form 的 material-ui 复选框
问题描述
我无法使用 react-hook-formreset
方法以编程方式设置 material-ui 复选框的值
这里有一个我的代码示例,您可以注意到,它TextField
可以正常工作,CheckBox
但不能!
https://codesandbox.io/s/cocky-shamir-8lqph?file=/src/App.js
解决方案
根据控制器示例和文档,您可以使用Controller
fromreact-hook-form
正确更改材质 UI 复选框的值。沙盒
第一个复选框(没有标签)如下:
<Controller
name="checkwithoutlabel"
control={control}
defaultValue={false}
rules={{ required: true }}
render={(props) => (
<Checkbox
onChange={(e) => props.onChange(e.target.checked)}
checked={props.value}
/>
)}
/>
FormControlLabel
组件应该写成这样:
<FormControlLabel
control={
<Controller
name={"checkwithlabel"}
control={control}
defaultValue={false}
render={(props) => (
<Checkbox
{...props}
checked={props.value}
onChange={(e) => props.onChange(e.target.checked)}
/>
)}
/>
}
label={"Checkbox with label"}
/>
推荐阅读
- javascript - Javascript FullCalendar 突出显示数组中的日期
- r - R中的Web抓取表-尽管存在2个表,但仍返回空表列表
- scala - 如何从 Avro 类列表中获取对象
- pine-script - 更新:可以在常规时段图表上显示盘前高点/低点吗?
- python - 检索 XML 文件中的拆分值
- ios - DeepLabV3Output XCode 13 的类型元数据访问器
- swift - 为什么参数标签在这个 Swift 代码中变得无关紧要?
- yii - YII2中视图文件中未定义的变量
- emacs - 使用 swank/slime 在远程 Lisp 中加载本地文件
- python - 需要从python opencv代码中找出哪个形状大哪个形状小