渲染订单问题(反应)

时间:2018-12-19 08:38:06

标签: reactjs paypal render setstate

我有一个Paypal组件,可以在成功付款时执行逻辑。

目标是首先执行paypal/axios.post,然后执行this.setState(...)

以下代码块在setState()逻辑之前执行Paypal ,并且从未付款:

render() {
    const onSuccess = payment => {
      axios
        .post(
          "http://compute.amazonaws.com:3000/ethhash",
          {userInput: this.props.userInput}
        )
        .then(response => console.log(response.data, payment))
            .catch(function(error) {
          console.log(error);
        });
    };

this.setState({ redirect: true });

if (this.state.redirect) {
      return <Redirect to="/FinishedPaying" userInput={this.state.userInput} />;
    }}

如果我将setState()嵌套在onSuccessaxios.post逻辑中,则会破坏我的(express)服务器。奇怪的是,我的服务器给我的错误是某个功能不是一个功能……命名的功能在所有其他条件下都可以工作。

我非常感谢您能为我提供帮助,欢呼:)

1 个答案:

答案 0 :(得分:0)

首先,您不应该在render函数中使用setState,而必须将其移动到React的其他生命周期,或者需要基于某些用户操作来调用发布请求。

onPayment= async ()=>{
 const res=await axios.post("http://compute.amazonaws.com:3000/ethhash",
          {userInput: this.props.userInput}
        )
   // check here if any error if error don't set the setState.

 this.setState({redirect:true})
}
相关问题