反应状态更新 ajax 成功

React state update in ajax success

当 ajax 调用成功 运行 时,我在设置状态时遇到问题。我想在 ajax 过程完成时更新状态。 div 中的文本保留在 "Busy" 而不是 "Done",而在浏览器网络选项卡中,我看到状态从 "pending" 变为状态“200”。

import React, { Component } from "react";
import * as ReactDOM from "react-dom";
import { extend } from "lodash";

export class StoreToCheck extends React.Component{
  constructor(props){
    super(props);
    this.state = { ListWithISBN :[],
                   getISBNS : false };
    this.ajaxSuccess = this.ajaxSuccess.bind(this);
  }
  getISBNSList(){
    if(!this.state.getISBNS){
      var store_name;
      // loop through array to fill store_name variable

      var ajaxSuccess = this.ajaxSuccess;
      if(store_name != ''){
        apex.server.process(
          'GET_EBOOKS_FROM_STORE',
          {
            success:function (data){
              // when succesfull update state getISBNS
              ajaxSuccess
            }
          }
        );
      }
    }
  }
  ajaxSuccess(){
    this.setState({"getISBNS":true});
  }
  componentDidMount(){
    this.getISBNSList();
  }
  render(){
    return(
      <div>
          {this.state.getISBNS ? "Done" : "Busy"}
      </div>
    )
  }
}

您需要 call ajaxSuccess 方法,也可以将其绑定到位

而不是存储正确的函数引用
getISBNSList(){
    if(!this.state.getISBNS){
      var store_name;
      // loop through array to fill store_name variable

      if(store_name != ''){
        apex.server.process(
          'GET_EBOOKS_FROM_STORE',
          {
            success: (data) => { // bind here with arrow function
              // when succesfull update state getISBNS
              this.ajaxSuccess() // call the function
            }
          }
        );
      }
    }
  }