score:3

Accepted answer

There are some issues in your code

  1. setState may be asynchronous, meaning state wont get updated immediately after setState is call. It is mentioned clearly in React docs: https://reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous
  2. You should never directly modify the state inside componentDidMount It is also mentioned here: https://reactjs.org/docs/state-and-lifecycle.html#do-not-modify-state-directly So I suggest modify the code as below


    componentDidMount() {
      this.getDeductionInfosList(data, function(response){
        this.createAppState({
            group: [{ field: "DeductionTypeTitle" }]}, response.Result, true)
      });
    }
    createAppState(dataState, items collapseAll) {
      const dataResult = process( items, dataState);
      if (collapseAll) {
        dataResult.data.forEach(dataItem => dataItem.expanded = false);
      }
      this.setState({
        dataResult: dataResult,
        dataState: dataState 
      });
    }


score:0

You have to transform the function to async and use await to the response.

async function getDeductionInfosList(data){ await GetRestrictionListService.getDeductionInfos(data, this.successGetDeductionInfos);}


Related Query

More Query from same tag