如何从嵌套组件中以编程方式强制实例化React组件

时间:2018-10-18 11:20:49

标签: reactjs typescript single-page-application react-router-v4

我正在使用Modal Form来添加数据库,以React.js和Typescript作为我的前端

由于“ this.props.history.push(“ / fetchcomponent”)'不会重定向回我想要构造函数的方式,因此我很难实现保存按钮,因为它应该可以正常工作再次加载数据...

我尝试将父组件的历史记录传递为props,但是我不确定这是否是问题所在?

这是嵌套模式组件内部的一种在单击时触发的方法:

private handleSave(event) {

        event.preventDefault();
        const data = new FormData(event.target);
        // PUT request for Edit naselje.  
        if (this.state.nasData.idnaselje) {
            fetch('api/Naselje/Edit', {
                method: 'PUT',
                body: data,
            }).then((response) => response.json())
                .then((responseJson) => {
                    this.props.history.push("/fetchnaselje"); // doesn't redirect back to the /fetchnaselje
                })
        }
        // POST request for Add naselje.  
        else {
            fetch('api/Naselje/Create', {
                method: 'POST',
                body: data,
            }).then((response) => response.json())
                .then((responseJson) => {
                    this.props.history.push("/fetchnaselje"); // doesn't redirect back to the /fetchnaselje
                })
        }
        this.props.parent.showModal();
        //window.location.reload() ??
    }

正在保存数据,但是我必须手动单击不同的路径,然后返回到/ fetchnaselje以显示正在添加的新数据,我该如何以编程方式执行此操作?

0 个答案:

没有答案