Max Gordon
Max Gordon

Reputation: 325

Update component UI while method is still running - React

So I have an onClick method that loops about 1,000 times before returning, and at the end of each loop, it changes the state. Is there a way to get the component to re-render at the end of each loop as well? I though just updating the state would do it, but that didn't work. I also tried calling this.forceUpdate() after updating the state every loop, but that didn't work either. It just does all of the updating all at once after onClick returns.

Is there a way to have the component update while the event handler is running? I am thinking the solution lies in multithreading, but I don't know if there is a better way.

Upvotes: 1

Views: 154

Answers (1)

Alexander Alexandrov
Alexander Alexandrov

Reputation: 1362

It mostly about event loop. And batching of setState in React. If you want to animate things https://en.reactjs.org/docs/animation.html

Upvotes: 1

Related Questions