简体   繁体   English

在 state 更改后反应组件未重新渲染,但正在调用 render()

[英]React component not rerendering after state change, but render() is being called

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.所以我有一个需要一段时间才能完成的方法(> 20 秒),它做了一堆数学和 CPU 繁重的工作,这就是为什么它需要这么长时间。 While it is running, it calls setState several times and the state correctly updates and render() is called.在运行时,它会多次调用setState ,并且 state 会正确更新并调用render() However, the component isn't rerendered on the web page, but it does rerender after the method finishes.但是,该组件不会在 web 页面上重新呈现,但会在方法完成后重新呈现。

I'm fairly sure that the method is pegging the CPU and React is not rerendering because of that.我相当确定该方法与 CPU 挂钩,因此 React 不会重新渲染。 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.这个片段可能不会在浏览器中显示它的意义,因为它使用了console.log ,我不知道你是否能看到。

It's not that it's pegging the CPU (though of course it could be pegging one of the cores), it's that there's only one browser thread used for running your JavaScript and updating the page display.并不是说它与 CPU 挂钩(当然它可能与其中一个内核挂钩),而是只有一个浏览器线程用于运行 JavaScript 并更新页面显示。 As long as your method is running, the browser can't update the display of the page.只要您的方法正在运行,浏览器就无法更新页面的显示。

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?或者有没有办法强制页面重新呈现?

To do that, you have to:为此,您必须:

  1. Break your function up into chunks and run each chunk separately, using setTimeout or similar to schedule the next chunk.将您的 function 分解成块并分别运行每个块,使用setTimeout或类似的方法来安排下一个块。 By doing that, you let the thread exit your JavaScript code and get used by the browser to repaint the page.通过这样做,您可以让线程退出您的 JavaScript 代码并被浏览器用来重新绘制页面。

    or或者

  2. Move your computationally-heavy code to a web worker ( spec , MDN ), so it can run in parallel with page updates, keeping your page responsive even though the method is doing heavy computation.将计算量大的代码移动到 web 工作程序(规范MDN ),这样它就可以与页面更新并行运行,即使该方法正在执行大量计算,也可以保持页面响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM