【问题标题】:Avoid form being reset避免表单被重置
【发布时间】:2020-03-25 10:50:20
【问题描述】:

我正在尝试 react-hook-forms。无论如何在提交时不重置表单?每次我点击提交表单都会重置。

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

import {
  Row,
  Col,
  Card,
  CardHeader,
  CardBody,
  Form,
  FormGroup,
  Label,
  Input,
  Button
} from "reactstrap";

const Insights = props => {
  const { register, handleSubmit, watch } = useForm();

  const GetSearchForm = () => {
    const timePickerStyle = { width: 96, important: "true" };

    const onSearch = data => {
      console.log(data);
    };

    return (
      <Form onSubmit={handleSubmit(onSearch)}>
        <Row>
          <Col>
            <FormGroup>
              <Label for="exampleEmail">Account Id</Label>
              <Input
                type="number"
                name="account"
                id="account"
                placeholder="AccountId"
                innerRef={register}
              />
            </FormGroup>
          </Col>
          <Col>
            <FormGroup>
              <Label for="examplePassword">Email</Label>
              <Input
                type="email"
                name="email"
                id="email"
                placeholder="Email"
                innerRef={register}
              />
            </FormGroup>
          </Col>

          <Col>
            <FormGroup>
              <Label for="exampleEmail">xPage Id</Label>
              <Input
                type="number"
                name="xpageid"
                id="xpage"
                placeholder="xPage Id"
                innerRef={register}
              />
            </FormGroup>
          </Col>
          <Col>
            <FormGroup>
              <Label for="examplePassword">Content Devider Id</Label>
              <Input
                type="number"
                name="contentdevider"
                id="contentdeviderid"
                placeholder="Content Devider Id"
                innerRef={register}
              />
            </FormGroup>
          </Col>
          <Col>
            <FormGroup>
              <Label for="examplePassword">Custom Page Id</Label>
              <Input
                type="number"
                name="custompage"
                id="custompageid"
                placeholder="Custom Page Id"
                innerRef={register}
              />
            </FormGroup>
          </Col>
          <Col>
            <FormGroup>
              <Label for="examplePassword">Service</Label>
              <Input
                type="text"
                name="servicename"
                id="servicename"
                placeholder="Custom Page Id"
                innerRef={register}
              />
            </FormGroup>
          </Col>
        </Row>

        <Row>
          <Col xs="4">
            <FormGroup>
              <Label for="exampleDate">Date</Label>
              <Row>
                <Col xs="8">
                  <Input
                    type="date"
                    name="date"
                    id="exampleDate"
                    placeholder="date placeholder"
                    innerRef={register}
                  />
                </Col>
                <Col xs="3">
                  <Input
                    style={timePickerStyle}
                    type="time"
                    name="time"
                    id="exampleTime"
                    placeholder="time placeholder"
                    innerRef={register}
                  />
                </Col>
              </Row>
            </FormGroup>
          </Col>
          <Col xs="4">
            <FormGroup>
              <Label for="exampleDate">Date</Label>
              <Row>
                <Col xs="8">
                  <Input
                    type="date"
                    name="date"
                    id="exampleDate"
                    placeholder="date placeholder"
                    innerRef={register}
                  />
                </Col>
                <Col xs="3">
                  <Input
                    style={timePickerStyle}
                    type="time"
                    name="time"
                    id="exampleTime"
                    placeholder="time placeholder"
                    innerRef={register}
                  />
                </Col>
              </Row>
            </FormGroup>
          </Col>
        </Row>

        <Button>Submit</Button>
      </Form>
    );
  };

  return (
    <Row>
      <Col xs="12" lg="12">
        <Card>
          <CardHeader>
            <i className="fa fa-align-justify"></i> Insights
          </CardHeader>
          <CardBody>
            <GetSearchForm></GetSearchForm>
          </CardBody>
        </Card>
      </Col>
    </Row>
  );
};

export default Insights;

【问题讨论】:

    标签: reactjs react-hook-form


    【解决方案1】:

    因为GetSearchForm 是它自己的组件,所以每次重新渲染Insights 时都会创建它。

    您使用 innerRef 调用 register 函数,但由于 Input 已更改,并且它不是同一个组件(新创建的),因此它是新注册的,这会重置状态。

    您可以将useForm 移动到GetSearchForm 并在提交时将数据备份或将整个表单内联到Insights

    【讨论】:

    • 做了第一个解决方案,它奏效了。非常感谢!
    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 2022-01-15
    • 2011-07-14
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多