0

I got an array and I neeed to go through, select the same items, push a new array and update a state

const uniqueNumberEvent = 'z3werbfhgdhhsbz';
for (var j = 0; j < this.state.events.length; ++j) {
    if (this.state.events[j].unique_number_event == uniqueNumberEvent) {
        selectedTickets.push(this.state.events[j].tickets)
    }
}

this.setState({ 
    tickets: selectedTickets,
});
console.log(this.state.tickets);

But this code create the new array but don't update the state

Edinho Rodrigues
  • 354
  • 4
  • 24
  • It does, you log before the state gets updated. – Jonas Wilms Jul 15 '19 at 17:21
  • 1
    `setState` is not synchrone so logging the state right its update does not give you what you expect. However, if your code is correct, try to log the results in the `render` method, it should give you the correct value. More info here: https://stackoverflow.com/questions/30782948/why-calling-react-setstate-method-doesnt-mutate-the-state-immediately – Gaël S Jul 15 '19 at 17:24

0 Answers0