简体   繁体   English

如何将 react-hook-form v7 与 reactstrap 9 一起使用?

[英]How to use react-hook-form v7 with reactstrap 9?

I'm currently trying to create a form with react-hook-form v7 and reactstrap 9, using react 18. Here's my current implementation but in my console logs, the data is returning an undefined .我目前正在尝试使用 react 18 创建一个带有 react-hook-form v7和 reactstrap 9 的表单。这是我当前的实现,但在我的控制台日志中,数据返回一个undefined If you guys could tell me how to properly integrate them, it would be helpful.如果你们能告诉我如何正确地整合它们,那将会很有帮助。 Thanks a lot!非常感谢!

  const { register, handleSubmit } = useForm();

  const onSubmit = (data) => {
    //here, the output is --> {email: undefined, password: undefined}
    console.log("sign in form data", data);
  };

// Sample Form
 <Form inline onSubmit={handleSubmit(onSubmit)}>
      <FormGroup className="mb-2">
        <Label className="auth-label" htmlFor="email">
          Email address*
        </Label>
        <Input
          id="email"
          name="email"
          placeholder=""
          type="email"
          className="auth-input"
          {...register("email")}
        />
      </FormGroup>
      <Button block className="auth-button mt-4">
        Login
      </Button>
 </Form>

Try to define register variable as follows:尝试按如下方式定义寄存器变量:

const { ref, ...sampleName } = register("name");

and use this ref inside Reactstrap Input并在 Reactstrap Input 中使用这个 ref

<Input innerRef={ref} {...sampleName}/>

Please see sandbox here: https://codesandbox.io/s/reactstrap-sandbox-forked-z2uxl?file=/src/index.js:808-842请在此处查看沙箱: https://codesandbox.io/s/reactstrap-sandbox-forked-z2uxl?file=/src/index.js:808-842

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM