【问题标题】:Resetting a reactjs form after submission提交后重置reactjs表单
【发布时间】:2020-07-06 12:06:21
【问题描述】:

我创建了一个 reactjs 表单,可以成功地将数据发送到 django rest api。我想在单击按钮后重置表单,但它一直给我一个错误,说“this.props.resetState() 不是一个函数。有什么办法可以重置之后创建的表单的所有输入提交?

import React, { Component } from "react";
import { Button, Form, FormGroup, Input, Label } from "reactstrap";

import axios from "axios";

import { API_URL } from "../constants";

class ClientForm extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            pk: 0,
            name: "",
            email: "",
            business_name: "",
            country: "",
        };
    }

    componentDidMount() {
        if (this.props.client) {
            const {
                pk,
                name,
                email,
                phone,
                business_name,
                country,
            } = this.props.client;
            this.setState(pk, name, document, email, phone);
        }
    }

    onChange = (e) => {
        this.setState({ [e.target.name]: e.target.value });
    };

    createClient = (e) => {
        e.preventDefault();
        axios.post(API_URL, this.state).then(() => {
            this.props.reset();
            this.props.toggle();
        });
    };

    editClient = (e) => {
        e.preventDefault();
        axios.put(API_URL + this.state.pk, this.state).then(() => {
            this.props.reset();
            this.props.toggle();
        });
    };

    defaultIfEmpty = (value) => {
        return value === " " ? " " : value;
    };

    render() {
        return (
            <Form onSubmit={this.props.client ? this.editClient : this.createClient}>
                <FormGroup>
                    <Label for="name">Name:</Label>
                    <Input
                        type="text"
                        name="name"
                        onChange={this.onChange}
                        value={this.defaultIfEmpty(this.state.name)}
                    />
                </FormGroup>
                <FormGroup>
                    <Label for="email">Email:</Label>
                    <Input
                        type="email"
                        name="email"
                        onChange={this.onChange}
                        value={this.defaultIfEmpty(this.state.email)}
                    />
                </FormGroup>
                <FormGroup>
                    <Label for="phone">Phone Number:</Label>
                    <Input
                        type="text"
                        name="phone"
                        onChange={this.onChange}
                        value={this.defaultIfEmpty(this.state.phone)}
                    />
                </FormGroup>
                <FormGroup>
                    <Label for="business_name">Business Name:</Label>
                    <Input
                        type="text"
                        name="business_name"
                        onChange={this.onChange}
                        value={this.defaultIfEmpty(this.state.business_name)}
                    />
                </FormGroup>
                <FormGroup>
                    <Label for="country">Country:</Label>
                    <Input
                        type="text"
                        name="country"
                        onChange={this.onChange}
                        value={this.defaultIfEmpty(this.state.country)}
                    />
                </FormGroup>
                <Button>Send</Button>
            </Form>
        );
    }
}

export default ClientForm;

【问题讨论】:

  • 你的“resetState”方法是做什么的?

标签: reactjs this reset react-props react-forms


【解决方案1】:

由于您在方法中使用controlled components,因此您可以将组件状态重置为initialState 值,然后在表单提交处理程序结束时调用this.setState(this.initialState)

类似:

class ClientForm extends React.Component {

    constructor(props) {
        super(props);
        this.initialState = {
            pk: 0,
            name: "",
            email: "",
            business_name: "",
            country: "",
        };
        this.state = this.initialState;
    }

    (...)

    handleSubmit = (e) => {
        e.preventDefault();        
        this.props.client ? this.editClient() : this.createClient();
        this.setState(this.initialState);
    }

    (...)

    render() {
        return (
            <Form onSubmit={this.handleSubmit}>
                (...)
            </Form>
        );
    }
}

export default ClientForm;

注意不要忘记删除e 参数和createClient()editClient() 方法中的e.preventDefault() 调用。

【讨论】:

  • 感谢您抽出宝贵时间提供帮助。我刚试过这个,它阻止了反应应用程序将数据发送到数据库。此外,输入的值仍然保留在表单上
  • 这完全有帮助。我省略了将 this.state 设置为 this.initialState。谢谢
猜你喜欢
  • 2018-06-20
  • 2013-03-03
  • 2018-10-05
  • 2021-09-08
  • 2021-07-02
  • 1970-01-01
  • 2020-09-06
  • 2020-04-10
  • 2021-09-12
相关资源
最近更新 更多