myT*_*532 2 reactjs reactstrap react-hook-form
我正在尝试添加react-hook-form到我的reactstrap表单和输入中,但看起来包之间存在冲突。
import React, { useState, useEffect } from 'react';
import { useForm } from "react-hook-form";
import {
Button,
Form,
Label,
Input
} from 'reactstrap';
import { FormControlLabel, Checkbox, FormGroup } from '@material-ui/core';
...
const { register, handleSubmit, errors } = useForm();
...
const updDetails = (data) => {
console.log(data);
}
return (
<Form onSubmit={handleSubmit(updDetails)}>
<FormGroup className="mr-10 mb-10">
<Label for="compName" className="mr-sm-10">Company Name</Label>
<Input type="text" name="compName" id="compName" placeholder="Company Name"
ref={register({ required: true })} aria-invalid={errors.name ? "true" : "false"}
value={compDetails.compName} onChange={(e) => setCompDetails({...compDetails, compName: e.target.value})}
/>
{errors.name && errors.name.type === "required" && (
<span role="alert">This is required</span>
)}
</FormGroup>
<Button type="submit" className="col-sm-3" color="primary">Update Details</Button>
</Form>
)
...
Run Code Online (Sandbox Code Playgroud)
当我加载页面时,我可以在浏览器控制台中看到所有输入的警告:Field is missing `name` attribute 。但是,我将名称添加到每个输入,如上面的代码所示。另外,当我单击它提交Update Details的空值时compName,数据是空对象。我不能使用react-hook-formwithreactstrap吗?
谢谢
您可能必须使用innerRef而不是在组件ref上Input。我在来源中找到了这个。
<Input
type="text"
name="compName"
id="compName"
placeholder="Company Name"
innerRef={register({ required: true })}
aria-invalid={errors.compName ? "true" : "false"}
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3494 次 |
| 最近记录: |