2

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?

2
  • 1
    setState is async, you cannot just console after the setState function Commented Apr 30, 2019 at 6:22
  • 1
    You can not do setState and expect the new state value in the next line. React documentation clearly mentions that setState will update the state whenever react finds it convenient. Commented Apr 30, 2019 at 6:27

1 Answer 1

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

You can check like this

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.