0

设置:我有一个将数据发送给动作创建者的表单,动作创建者又提交给 API 并获取结果。我想要的是当表单提交成功时,用空白输入刷新表单。

这是组件的样子

import React, { Component } from "react";
import { bindActionCreators } from "redux";
import { connect } from "react-redux";
import { addNewProduct } from "../../redux/actions";

class Admin extends Component {

    state = {
        ProductName: ""
    };

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

    handleProductSubmit = (event) => {
        event.preventDefault();
        this.props.addNewProduct(
            this.state.ProductName,
        );
    }

    render() {
        return (
            <div>
                {/* Form ends */}
                <form onSubmit={this.handleProductSubmit} autoComplete="off">

                        <input
                            type="text"
                            value={this.state.ProductName}
                            name="ProductName"
                            onChange={this.onChange}
                        />

                    <button type="submit" className="btn btn-dark">
                        Upload Product
                    </button>
                </form>
                {/* Form Ends */}

            </div>
        );
    }
}

const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({ addNewProduct, createNewLogin }, dispatch);
};

export default connect(null, mapDispatchToProps)(Admin);

这是 console.log(this.props) 的结果

location: Object { pathname: "/Home/admin", href: "http://localhost:3000/Home/admin", origin: "http://localhost:3000", … }
navigate: navigate(to, options)
​​
length: 2
​​
name: "navigate"
​​
prototype: Object { … }
​​
<prototype>: ()

这就是 actionCreator 的样子

export const addNewProduct = (ProductName, ProductCategory, ProductImg) => (dispatch) => {

    const productData = new FormData();
    productData.append("ProductName", ProductName)
    axios.post("http://localhost:4500/products/", productData,
        {
            headers: {
                "Content-Type": "multipart/form-data",
                "Authorization": localStorage.getItem("Authorization")
            }
        })
        .then(res => {
            console.log(res.data)
            setTimeout(() => {
                console.log("doing the timeout")
                navigate("/Home/admin")}, 1500);

        })
        .catch(err =>
            console.log(`The error we're getting from the backend--->${err}`))
};

当前行为

当我提交表单并且 API 返回 201 时,页面不会刷新并且输入不会变为空白

预期行为

当我从 API 获得 201 时,页面应该刷新并且输入应该是空白的。

请帮助我如何实现这一目标。

4

1 回答 1

0

使用导航移动相同的 url 或页面不会重新安装页面并重置您的字段值。

更好的是你实际上从你的动作创建者那里返回一个承诺并自己重置状态

export const addNewProduct = (ProductName, ProductCategory, ProductImg) => (dispatch) => {

    const productData = new FormData();
    productData.append("ProductName", ProductName)
    return axios.post("http://localhost:4500/products/", productData,
        {
            headers: {
                "Content-Type": "multipart/form-data",
                "Authorization": localStorage.getItem("Authorization")
            }
        })
        .then(res => {
            console.log(res.data)   
        })
        .catch(err =>
            console.log(`The error we're getting from the backend--->${err}`))
};

在组件中

handleProductSubmit = (event) => {
    event.preventDefault();
    this.props.addNewProduct(
        this.state.ProductName,
    ).then(() => {
        this.setState({ProductName: ""})
    });
}
于 2020-05-09T10:29:08.097 回答