到达路由器刷新页面

问题描述 投票:0回答:1

设置:我有一个表单,可以将数据发送到一个动作创建者,而这个动作创建者又会提交到一个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时,页面应该刷新,输入应该是空白。

请帮助我如何实现这一点。

javascript reactjs react-router react-router-dom reach-router
1个回答
0
投票

使用navigate移动相同的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: ""})
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.