【问题标题】:Redirect after a post request with data in react js / node js使用 react js / node js 中的数据在发布请求后重定向
【发布时间】:2019-05-27 12:17:00
【问题描述】:

在向我的节点 js 服务器发出发布请求后,我正尝试将数据发送到另一个组件页面。

要重定向的第一个组件:(我无法发布所有代码,它太大了

fetch('http://localhost:8080/createVolume', requestOptions)
                .then(response => response.json())
                .then(data => {
                    console.log(data);
                    this.setState({
                        ansibleAnswer: data,
                        submitted: true
                    })
                    console.log(this.state.ansibleAnswer);
                });
.
.
.
// uderneeth my render

        } else if (this.state.loaded === true && this.state.submitted === true) {
            return (
                <Redirect to={{
                    pathname: '/Completed',
                    state: { name: this.state.name, result: this.state.ansibleAnswer }
                }}
                />
            );

发布请求后的新组件:

class Completed extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            name: this.props.location.state.name,
            result: this.props.location.state.ansibleAnswer
        };
    }

    render() {
        return (
            <div>
                <Header />
                <div>
                    <p>test successful</p>
                    <p>{this.state.result}</p>
                </div>
                <Footer />
            </div>
        );
    }
}


export default Completed;

我得到这个错误:

警告:无法对未安装的组件执行 React 状态更新。这是一个空操作,但它表明您的应用程序中存在内存泄漏。要修复,请在 componentWillUnmount 方法中取消所有订阅和异步任务。

我了解数据未保存在状态中,但我不明白如何在将数据发送到其他组件之前保存(或等待)它。

谢谢

【问题讨论】:

    标签: node.js reactjs


    【解决方案1】:
            class Completed extends React.Component {
                constructor(props) {
                    super(props);
                    this.state = {
                        name: '',
                        result: ''
                    };
                }
    
            /** When the Completed component does anything with the new props,
            * componentWillReceiveProps is called, with the next props as the argument */
    
            componentWillReceiveProps(nextProps) {
    
                /** Update  state**/
    
                if (nextProps.location.state.result !== 
                this.props.location.state.result) {
                this.setState({ 
                    result:nextProps.location.state.result 
                    });
                }
    
            }
    
                render() {
                    return (
                        <div>
                            <Header />
                            <div>
                                <p>test successful</p>
                                <p>{this.state.result}</p>
                            </div>
                            <Footer />
                        </div>
                    );
                }
            }
    
    
            export default Completed;
    

    【讨论】:

    • 您好,谢谢您的回答,但是不知道为什么,字符串还是空的。
    • 尝试在componentWillReceiveProps方法中记录props,然后检查“nextProps.location.state.ansibleAnswer”是否有数据
    • nextProps.location.state.ansibleAnswer 应该是 nextProps.location.state.result ......检查你的重定向状态道具
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    相关资源
    最近更新 更多