简体   繁体   English

使用材质 UI 按钮时,react-hook-form 不起作用

[英]react-hook-form doesn't work when using material UI button

This is my code without material UI button:这是我没有材料 UI 按钮的代码:

const {register, handleSubmit} = useForm();

const onSubmit = (data) => {
    console.log(data)
}
const handleChange = (e) => {
    console.log(e.target.files)
}

...

<form id="myFile" onSubmit={handleSubmit(onSubmit)}>
    <input id="file1" type="file" {...register("file1")} onChange={handleChange}/>
    <input type="submit"/>
</form>

This works for me, but when I try to add material UI button instead of input, I get onChange value but when I click submit.这对我有用,但是当我尝试添加材料 UI 按钮而不是输入时,我得到了 onChange 值,但是当我单击提交时。 I don't get any form data.我没有得到任何表格数据。

const {register, handleSubmit} = useForm();

const onSubmit = (data) => {
    console.log(data)
}
const handleChange = (e) => {
    console.log(e.target.files)
}

...

<form id="myFile" onSubmit={handleSubmit(onSubmit)}>
    <input id="file1" type="file" {...register("file1")} onChange={handleChange} 
    style={{display:"none"}}/>
    <label htmlFor="file1">
        <Button variant="contained" component="span">
             Choose file
        </Button>
    </label>
    <input type="submit"/>
</form>

Is there any solution here?这里有什么解决办法吗?

You are forget to mention the type of the button您忘记提及按钮的类型

for Default material ui button type is默认材质 ui 按钮类型为

type="button"

Check this git Document 检查此 git 文档

you should mention你应该提到

type="submit"

So do like this所以这样做

<Button type="submit" variant="contained" component="span">
             Choose file
</Button>

You can try something like this:你可以尝试这样的事情:

import React, { useState } from 'react';
import { Button, TextField } from '@material-ui/core';
import useForm from 'react-hook-form';
import { object, string } from 'yup';

const Form: React.FC = () => {
  const schema = object().shape({
    username: string().required('username required'),
    password: string().required('password required'),
  });
  const { register, handleSubmit, errors } = useForm({ validationSchema: schema });

  const onSubmit = (data: any) => {
    console.log(data);
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <TextField
        name="username"
        error={!!errors.username}
        label="Username"
        helperText={errors.username ? errors.username.message : ''}
        type="email"
        inputRef={register}
        fullWidth
      />
      <TextField
        name="password"
        error={!!errors.password}
        label="Password"
        inputRef={register}
        helperText={errors.password ? errors.password.message : ''}
        type="password"
        fullWidth
      />

      <Button
        color="secondary"
        type="submit"
        variant="contained"
        fullWidth
      >
        Submit
      </Button>
    </form>
  );
};

ref: How to use react-hook-form with ant design or material UI ref: 如何在 ant 设计或材料 UI 中使用 react-hook-form

Currently your submit event is controlled by the whole form element <form onSubmit={handleSubmit(onSubmit)}> but this is possible only when the form has a relevant submit button like <input type="submit"/> but when you use materail ui, MUI button doesnt distincts submit type even if mentioned.目前,您的提交事件由整个表单元素<form onSubmit={handleSubmit(onSubmit)}>控制,但这只有在表单具有相关的提交按钮(如<input type="submit"/>但是当您使用 materail 时才有可能ui,即使提到,MUI 按钮也不会区分提交类型。 So the solution is to move your onSubmit function at form to down the onClick event of your new MUI button.因此,解决方案是将表单上的onSubmit function 移动到新 MUI 按钮的onClick事件下。 The code that works is:有效的代码是:

<Button onClick={handleSubmit(onSubmit)} type="submit" variant="contained" component="span"> Submit </Button>

暂无
暂无

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

相关问题 MUI 自动完成功能不适用于 react-hook-form - MUI Autocomplete doesn't work with react-hook-form 将 MUI DatePicker 与 yup 和 react-hook-form 一起使用 - error prop 无法按预期工作 - Using the MUI DatePicker with yup and react-hook-form - the error prop doesn't work as intended 带有 react-hook-form 的 Material UI (MUI) 日期选择器 - Material UI (MUI) date picker with react-hook-form 使用 react-hook-form 重置功能时,Material-UI 复选框不会重置 - Material-UI Checkbox is not reset while using react-hook-form reset function 使用 Material UI 和 react-hook-form 创建可重用的文本字段组件 - Creating reusable text field component using Material UI and react-hook-form 为什么未使用 react-hook-form 在 Material UI Autocomplete 中设置初始值? - Why is initial value not set in Material UI Autocomplete using react-hook-form? 与 react-hook-form 一起使用时,自定义 React 组件不显示输入长度 - Custom React component doesn't show input length when used with react-hook-form 使用 react-hook-form 和 react-dropzone 时 setValue 不起作用 - setValue does not work when using react-hook-form and react-dropzone 将 react-hook-form 与 material-ui (mui) 结合使用:如何在规则验证中使用输入中的必需标志? - Using react-hook-form with material-ui (mui): How do I use the required flag on inputs in with the rules validation? 使用 react-hook-form 的 Controller 组件不允许自定义 Antd Select 显示标签 - Using Controller component of react-hook-form doesn't allow custom Antd Select to show label
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM