简体   繁体   English

使用<TextField />带有 react-hook-form 的 material-ui 组件显示警告

[英]Using <TextField /> component from material-ui with react-hook-form shows warning

I am using react-hook-form for form state management in my application.我在我的应用程序中使用 react-hook-form 进行表单状态管理。 When I am using <Input /> as a control, it works as expected, however with <TextField /> it shows a warning saying "A component is changing an uncontrolled input of type text to be controlled."当我使用<Input />作为控件时,它按预期工作,但是使用<TextField />它会显示警告说“组件正在更改要控制的文本类型的不受控制的输入”。 What's going wrong here?这里出了什么问题? Is there any alternative for this component?这个组件有什么替代品吗?

Here is my react code:这是我的反应代码:

import React from "react";
import "./styles.css";
import { useForm, Controller } from "react-hook-form";
import Joi from "@hapi/joi";
import { TextField, createMuiTheme, ThemeProvider } from "@material-ui/core";

const validationSchema = Joi.object({
  username: Joi.string()
    .alphanum()
    .min(3)
    .max(30)
    .required()
});

const theme = createMuiTheme({
  palette: {
    type: "dark"
  }
});

const resolver = (data, validationContext) => {
  const { error, value: values } = validationSchema.validate(data, {
    abortEarly: false
  });

  return {
    values: error ? {} : values,
    errors: error
      ? error.details.reduce((previous, currentError) => {
          return {
            ...previous,
            [currentError.path[0]]: currentError
          };
        }, {})
      : {}
  };
};

export default function App() {
  const { register, handleSubmit, errors, control } = useForm({
    validationResolver: resolver,
    validationContext: { test: "test" }
  });

  console.log("error", errors);

  return (
    <ThemeProvider theme={theme}>
      <div className="App">
        <h1>Hello CodeSandbox</h1>

        <form onSubmit={handleSubmit(d => console.log(d))}>
          <label>Username</label>
          <Controller as={<input />} name="username" control={control} />
          <Controller
            as={<TextField />}
            name="firstName"
            label="First Name"
            control={control}
          />
          <input type="submit" />
        </form>
      </div>
    </ThemeProvider>
  );
}

and here is a link to it in a sandbox: https://codesandbox.io/s/react-hook-form-validationresolver-7k33n这是沙箱中的链接: https : //codesandbox.io/s/react-hook-form-validationresolver-7k33n

You can fix the warning by supplying default values to your input elements to prevent them from being undefined initially:您可以通过为输入元素提供默认值来修复警告,以防止它们最初未定义:

 <Controller
  as={<input />}
  name="username"
  control={control}
  defaultValue=""
/>
<Controller
  as={<TextField />}
  name="firstName"
  label="First Name"
  control={control}
  defaultValue=""
/>

暂无
暂无

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

相关问题 Material-UI 使用 react-hook-form 的 setValue 时,TextField 未更新值 - Material-UI TextField is not updating value when using setValue of react-hook-form React-hook-form with Material-ui textfield without autoFocus defaultValue 在表单提交过程中消失了 - React-hook-form with Material-ui textfield without autoFocus defaultValue disappeared during form submit Material-UI TextField state 在使用 react-hook-form 进行不受控制的值更改后不正确 - Material-UI TextField state incorrect after uncontrolled value change with react-hook-form 使用 material-ui TextField 处理最新的 react-hook-form 错误 - latest react-hook-form error handling with material-ui TextField 使用 react-hook-form 和 Material-UI 解决问题 - React problem with react-hook-form and Material-UI react-hook-form 和 material-ui FormControl - react-hook-form and material-ui FormControl 使用带有日期/时间选择器的 react-hook-form 并使用 Material-UI 的示例? - Example of using react-hook-form with a Date/Time Picker and using Material-UI? 使用 react-hook-form 重置功能时,Material-UI 复选框不会重置 - Material-UI Checkbox is not reset while using react-hook-form reset function react-hook-form material-ui (FormControlLabel + Checkbox) 使用 Controller - react-hook-form material-ui (FormControlLabel + Checkbox) using Controller 使用带有 typescript 和 material-ui 的 react-hook-form 来显示错误消息的正确方法 - Right way of using react-hook-form with typescript and material-ui for showing error message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM