简体   繁体   English

使用 react-hook-form 和 Material-UI 解决问题

[英]React problem with react-hook-form and Material-UI

I am trying to construct a form with a dynamic field (and a switch to show/hide it).我正在尝试构建一个带有动态字段的表单(以及一个显示/隐藏它的开关)。 I want to use both react-hook-forms (for its great features: (1) automatically populate the data structure in the way I want, without me having to do it manually, and (2) the feature of 'watching' input (ie reacting to user actions immediately and reflecting this in a variable). I also want to use material-ui, because I need my app to look really modern. According to documentation of react-hook-forms it supports UI libraries, and material-ui is one of them.我想同时使用 react-hook-forms(因为它的强大功能:(1)以我想要的方式自动填充数据结构,而无需我手动完成,以及(2)“观看”输入的功能(即立即对用户操作做出反应并在变量中反映这一点)。我还想使用 material-ui,因为我需要我的应用程序看起来非常现代。根据 react-hook-forms 的文档,它支持 UI 库和 material- ui 就是其中之一。

But I have a problem.但我有一个问题。 For some kind of inputs, like material-ui's TextField, everything works fine.对于某些类型的输入,例如 material-ui 的 TextField,一切正常。 I can use 'watch' for it, it works perfectly.我可以使用“手表”,它完美无缺。 But for some others inputs, like Switch or Checkbox, the watch feature doesn't work well.但是对于其他一些输入,例如 Switch 或 Checkbox,监视功能不能很好地工作。

For example, in this particular test example, the page reacts well when I type something in the TextField (I can see it also in console), the watch displays correct value.例如,在这个特定的测试示例中,当我在 TextField 中输入内容时页面反应良好(我也可以在控制台中看到它),手表显示正确的值。 But when I click the Switch element, something strange happens.但是当我单击 Switch 元素时,发生了一些奇怪的事情。 The first time it works OK, my dynamic field appears, var isDog is set to true which is OK.第一次运行正常,我的动态字段出现了,var isDog设置为true就OK了。 But for the second time even though isDog value is false the code (the conditional rendered field) behave as if it was still true, and the result is that the dynamic field is rendered forever.但是第二次,即使 isDog 值是假的,代码(条件渲染字段)的行为就好像它仍然是真的一样,结果是动态字段被永远渲染。 Like it was cached somewhere or I don't know.就像它被缓存在某个地方或者我不知道。 What is going on here?这里发生了什么?

I tried using different 'value' in the code, eg "true", or place the value attribute in RFHInput itself, rather than in Switch (controlled by FormControlLabel), but nothing helps.我尝试在代码中使用不同的“值”,例如“true”,或者将 value 属性放在 RFHInput 本身中,而不是放在 Switch(由 FormControlLabel 控制)中,但没有任何帮助。

Anyone had the same problem?有人遇到过同样的问题吗? What is wrong here?这里有什么问题? Is this a bug?这是一个错误吗?

import React from "react";
import { useForm } from 'react-hook-form'
import Switch from '@material-ui/core/Switch';
import TextField from '@material-ui/core/TextField';
import { RHFInput } from 'react-hook-form-input';
import Button from '@material-ui/core/Button';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import ReactDOM from 'react-dom';

export default function App() {
   const { register, handleSubmit, watch, setValue } = useForm()

   const onSubmit = data => {
      console.log(data)
   }
   var isDog = watch("dog", false)
   var nameOfPet = watch("petname", "noname")
   console.log("isDog = " + isDog)  
   console.log("nameOfPet = " + nameOfPet)  
   return (
      <form onSubmit={handleSubmit(onSubmit)}>

         <RHFInput as={<TextField variant="outlined" margin="normal" required fullWidth label="name of your pet" autoFocus />}
               register={register} setValue={setValue} name="petname" />
         <RHFInput as={<FormControlLabel control={<Switch value={!isDog} />} label="dog" />}
            register={register} setValue={setValue} name="dog" />             

         <h2>Name of your pet is "{nameOfPet}" </h2>
         {isDog && (<RHFInput as={<TextField variant="outlined" margin="normal" required fullWidth label="how many legs does your dog have?" autoFocus />}
               register={register} setValue={setValue} name="petname" />)}
         {/* {!isDog && (<h1>It is not a dog!</h1>)} */}
         <Button type="submit" fullWidth variant="contained" color="primary" > Submit </Button>

       </form>
   );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Here is the example in codesandbox Link to codesandbox这是代码和框中的示例链接到代码和框

It seems that RHFInput have some bugs when combined with Material-UI Switch.与 Material-UI Switch 结合使用时, RHFInput似乎存在一些错误。

Try to remove it and change the register ref to inputRef尝试删除它并将register ref 更改为inputRef

<FormControlLabel
  control={<Switch inputRef={register} name="dog" />}
  label="dog" 
/>

Also, please note you have to change the second input name in order to get its value.另外,请注意您必须更改第二个输入名称才能获得其值。

Here is your codesandBox example.这是您的代码和框示例。

<FormControlLabel
  control={<Switch onChange={register('active').onChange}
                inputRef={register('active').ref}
 />}
  label="dog" 
/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 react-hook-form 和 material-ui FormControl - react-hook-form and material-ui FormControl React - Material-UI - 如何在 react-hook-form 中使用 Select 和多个值 - React - Material-UI - How to use Select with multiple values inside react-hook-form react-hook-form material-ui 表单只提交一次作品 - react-hook-form material-ui Form submit works only once React-hook-form with Material-ui textfield without autoFocus defaultValue 在表单提交过程中消失了 - React-hook-form with Material-ui textfield without autoFocus defaultValue disappeared during form submit 使用<TextField />带有 react-hook-form 的 material-ui 组件显示警告 - Using <TextField /> component from material-ui with react-hook-form shows warning 使用 react-hook-form 重置功能时,Material-UI 复选框不会重置 - Material-UI Checkbox is not reset while using react-hook-form reset function 使用 material-ui 进行 react-hook-form 验证 - 未插入第一个击键 - react-hook-form validation with material-ui - first keystroke not inserted 使用带有日期/时间选择器的 react-hook-form 并使用 Material-UI 的示例? - Example of using react-hook-form with a Date/Time Picker and using Material-UI? 带有 material-ui 的 React-hook-form 不会在 onBlur() 方法中保持值变化 - React-hook-form with material-ui does not keep value change in onBlur() method 带有 material-ui 的 React-hook-form 不显示来自规则的错误 - React-hook-form with material-ui does not show errors from rules
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM