So I have a method that takes a while to finish (>20 seconds), its doing a bunch of math and CPU heavy stuff, that's why it takes so long. While it is running, it calls setState several times and the state correctly updates and render() is called. However, the component isn't rerendered on the web page, but it does rerender after the method finishes.
I'm fairly sure that the method is pegging the CPU and React is not rerendering because of that. Is there a way to pause the execution of the method for a little bit in order for the webpage to rerender? Or is there a way to force the page to rerender?
class FibonacciPage extends React.Component {
constructor(props) {
super(props)
this.state = {
n: 0
}
// compute to 1000th place
this.nth = 1000
}
fibonacci(nth) {
console.log(nth)
this.setState({
n: nth
})
if (nth < 2) {
return 1
} else {
return this.fibonacci(nth - 1) + this.fibonacci(nth - 2)
}
}
componentDidMount() {
this.fibonacci(this.nth)
}
render() {
console.log("RENDER")
return ( <p> {this.state.n} </p>
)
}
}
ReactDOM.render(<FibonacciPage />, document.querySelector("#app"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>
This snipped may not show its point in the browser because it uses console.log and I don't know if you can see that.
renderis being called while your function is running; that would suggest React was calling it duringsetState, which it doesn't to my knowledge. Please update your question with a minimal reproducible example demonstrating the problem, ideally a runnable one using Stack Snippets (the[<>]toolbar button). Stack Snippets support React, including JSX; here's how to do one.render()is definitely being called as I haveconsole.loglines everywhere. I will update my question in a bit.fibonacci, but not the one inrender.