简体   繁体   English

使用WebWorkers呈现React组件

[英]Rendering React components with WebWorkers

I saw some stuff online about rendering React components with HTML5 WebWorkers..even Pete Hunt the head honcho behind React was talking about it. 我在网上看到了一些关于使用HTML5 WebWorkers呈现React组件的东西。即使是Pete Hunt,React正在谈论它。

I have some CPU intensive work over each element of an array followed by a React render of each element of the array, so I am thinking about putting each of those in a WebWorker and then posting the HTML string back to the main UI thread. 我对数组的每个元素进行了一些CPU密集型工作,然后对数组的每个元素进行React渲染,因此我考虑将每个元素放在WebWorker中,然后将HTML字符串发布回主UI线程。

My question is - it looks like React.renderToString is deprecated in favor of ReactDOMServer.renderToString ...so I ask all of you and Pete Hunt - are we supposed to use ReactDOMServer on the front-end if we wish to use WebWorkers to render React components or is there another recommended approach? 我的问题是 - 看起来不推荐使用React.renderToString来支持ReactDOMServer.renderToString ...所以我问你们所有人和Pete Hunt - 如果我们希望使用WebWorkers来渲染我们应该在前端使用ReactDOMServer反应组件还是有另一种推荐的方法?

(The reason of course, is we can only pass strings / serialized data between threads in JS, so the idea is to render the React component to a string, then pass it back to the main UI thread as a string.) (当然,我们只能在JS中的线程之间传递字符串/序列化数据,因此我们的想法是将React组件呈现为字符串,然后将其作为字符串传递回主UI线程。)

I wrote up a quick implementation of React, rendering in a Web worker. 我写了一个React的快速实现,在Web worker中呈现。 It is not really a renderToString , but more like a custom renderer. 它实际上不是renderToString ,而更像是自定义渲染器。 I also found that this is much faster than the normal implementation. 我还发现这比正常实现要快得多。

The demo page has 2 applications - an example showing a CPU intensive app, and a simple TODO list with events. 该演示页面有2个应用程序 - 一个显示CPU密集型应用程序的示例,以及一个包含事件的简单TODO列表。

Here are the performance numbers too - http://blog.nparashuram.com/2016/02/using-webworkers-to-make-react-faster.html 以下是性能数据 - http://blog.nparashuram.com/2016/02/using-webworkers-to-make-react-faster.html

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

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