【问题标题】:Using react-hook-form in React with Typescript in child component在 React 中使用 react-hook-form 和子组件中的 Typescript
【发布时间】:2021-06-25 07:47:46
【问题描述】:

我正在使用 react-hook-form 库的一个基本示例,即使在查找文档之后,我也不知道如何将数据从表单传递到另一个组件。这是我的表单组件:

import { useForm, SubmitHandler } from "react-hook-form";

type FormInputs = {
    minimalFrequency: number;
    maximialFrequency: number;
  };

// const onSubmit: SubmitHandler<FormInputs> = data => console.log(data);

export default function BasicUsage() {
  const { register, formState: { errors }, handleSubmit, getValues } = useForm<FormInputs>({
    defaultValues: {
        min: 250,
        max: 8000,
      }
  });
  const onSubmit = (data: any) => {
    console.log(data);
  }


  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register("minimalFrequency", { required: true })} />
      {errors.minimalFrequency && "Only numbers are allowed"}
      <input {...register("maximialFrequency", { required: true })} />
      {errors.maximialFrequency && "Only numbers are allowed"}
      <input type="submit" />
    </form>
  );
}

我想在用户按下“提交”按钮后以给定数据对象的形式获取用户的最小值和最大值,但我无法理解它是如何工作的。

我的主要组件是一个相当大的类组件,我读到它可能不起作用,因为 react-hook-form 需要一个功能组件。如果属实,有没有办法以某种方式仍然使用我的类组件?

更新:添加了父组件

import { useState } from "react";
import React from "react";
import BasicUsage from "./BasicUsage"

type Props = {

}

type State = {
  dataFreq: object;
}



export default class Parent extends React.Component<Props, State>{
    private timer: any;

constructor(props: Props) {
  super(props);
  this.state = {

    dataFreq: {
      minimalFrequency: 250,
      maximialFrequency: 8000
  }
  };
}

getDataFromForm = (dataFreq: any) => {
  this.setState({dataFreq: dataFreq })
  console.log(dataFreq)

};
render() {
  const minFreq  = this.state.dataFreq;

  console.log("This is a this dataFreq", this.state.dataFreq);
  console.log("This is a this minimalFrequency", minFreq);

  return (
    <div>
      <BasicUsage getDataFromForm={this.getDataFromForm}/>
    </div>
  );
}
}

【问题讨论】:

    标签: reactjs typescript react-hooks react-hook-form


    【解决方案1】:

    您仍然可以将您的类组件用作父组件。

    如果我假设您想在主组件中使用表单中的数据是正确的,并且主组件是父组件,您可以在主组件中定义一个函数,例如

    getDataFromForm(data){
    this.setState({data: data })
    }
    

    然后你将这个函数传递给你的BasicUsage 组件

    //In your main components render function, or wherever you are using the BasicUsage component
      <BasicUsage
        //other props you want to send into BasicUsage from the main component
        getDataFromForm={getDataFromForm}
      />
    

    现在,在您的 BasicUsage 组件的 onSubmit 函数中,您可以调用作为道具传递的函数

         const onSubmit = (data: any) => {
            //Do something with your data if you want to change format or process it somehow; 
    //in this case you should probably make a new variable and pass the new variable into getDataFromForm
            props.getDataFromForm(data) //Call the function in the parent component
          }
    

    如果您在同级组件而不是父组件中使用表单数据,您可以在公共父组件中创建 getDataFromForm 函数,并将函数传递给 BasicUsage 组件,并将 state.data 值传递给您要访问数据的兄弟组件

    【讨论】:

    • 感谢您的回答。如何访问主组件中的数据对象? console.log("This is a this dataFreq", this.state.dataFreq); 为我获取了不断变化的 dataFreq 对象,但我无法通过调用 console.log("This is a this dataFreq", this.state.dataFreq.minimalFrequency); 获取对象中的 minimumFrequency 值
    • 我不熟悉 react-hook-form 但可能在&lt;form onSubmit={handleSubmit(onSubmit)}&gt; 中您可以将其更改为&lt;form onSubmit={(e) =&gt; handleSubmit(e)}&gt; 并查看进入 onSubmit 函数的数据?
    • 我更改了函数getDataFromForm = (dataFreq: any) =&gt; { this.setState({dataFreqMin: dataFreq.minimalFrequency }) this.setState({dataFreqMax: dataFreq.maximalFrequency }) },它起作用了。还将类型添加到父级。
    • 如果我将 getDataFromFrom 设置为父级并从 child2 调用它,而 child1 是 BasicUsage 的父组件,您能否解释一下它是如何工作的。如果我定义一个父组件并导出该函数,我会收到未定义的错误。我的父组件就是你上面描述的功能和一个导出,以便我可以在其他组件中使用它
    • 不要导出函数,只需将其作为道具传递给您想要使用它的任何孩子。您可以继续将其作为道具向下传递给孙子、曾孙等,称为“道具钻孔”。我不认为该函数本身可以作为一个组件工作,它需要是一个具有状态的类组件才能使用 setState 。 (顺便说一句,我强烈建议研究并开始使用钩子和函数组件。这是一种相当容易的过渡,也是一种编写 React 代码的更舒适的方式)
    猜你喜欢
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2021-11-21
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    相关资源
    最近更新 更多