【问题标题】:Not getting any data back没有取回任何数据
【发布时间】:2022-01-21 04:56:15
【问题描述】:
import React, { useState } from 'react'

从“react-hook-form”导入 {useForm}

const SubmittionBox = () => {

const{reg, handleSubmit} = useForm()

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


return (
<form onSubmit={handleSubmit(onSubmit)} ref={reg}>
    
       <input type="text" placeholder="Type Name Here"  name ="name" required ref={reg}/>
       <input type="Submit"/>
   
</form>
)

}

导出默认提交框

当我点击提交按钮时,我返回的唯一内容是对象,而不是我放入文本框中的数据

【问题讨论】:

标签: reactjs


【解决方案1】:

试试这个

function App() {
  const { register, handleSubmit } = useForm();

  const onSubmit = (data) => {
    console.log(data);
  };
  return (
    <div className="App">
      <form onSubmit={handleSubmit(onSubmit)}>
        <input
          type="text"
          {...register("name")}
          placeholder="Type Name Here"
          required
        />
        <input type="Submit" />
      </form>
    </div>
  );
}

React Hook Form 中的一个关键概念是将不受控制的组件注册到钩子中。这将使其值可用于表单验证和提交。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 2020-10-08
    相关资源
    最近更新 更多