kinnu
kinnu

Reputation: 388

Unable to set the value of a variable to state variable in react js

I am getting API response and once after getting that I am applying some filter and want to assign the result to state variables.

Here is what I am trying to do:

filtered = this.state.result
      .filter(value => value.Date == bar.Date)
      .reduce((acc, e) => acc.concat(e.FailureDeatils), []);
    this.setState({ failureResultValue: filtered });<-- Here I am able to set the filtered to failureResultValue.

Again on this failureResultValue,I am applying filter and able to achieve the result i.e.

failureResultTransactions = this.state.failureResultValue
      .filter(data => data.name == pie.name)
      .reduce((acc, item) => acc.concat(item.TransactionDetails) , []).map(item => ({ ...item, checked: false }));
console.log(failureResultTransactions );<- It has value
this.setState({ items: failureResultTransactions });<- This is not working
console.log(this.state.items)<- is returning empty

Can anyone figure out where I am going wrong?

Upvotes: 2

Views: 1089

Answers (1)

Narendra Chouhan
Narendra Chouhan

Reputation: 2319

 this.setState({ items: failureResultTransactions },()=>
    console.log(this.state.items));

You can check like this

Upvotes: 1

Related Questions