简体   繁体   English

React v16 hydrate 和 renderToNodeStream 与 render 和 renderToString

[英]React v16 hydrate and renderToNodeStream vs render and renderToString

I've read about the differences between hydrate and render and also between renderToNodeStream and renderToString and then I tried to create a sample project to demonstrate the difference.我已经阅读了 hydrate 和 render 之间以及 renderToNodeStream 和 renderToString 之间的差异,然后我尝试创建一个示例项目来演示差异。 From all the tests I've done I couldn't find any difference in the behavior between these new functions.从我所做的所有测试中,我找不到这些新函数之间的行为有任何差异。

For example, I've created a simple Hello World program where the "World" is a parameter of my App component and both my server.js and browser.js uses the shared App component to set the parameter (in the server the parameter value set to "World" and in the browser the parameter value set to "Me").例如,我创建了一个简单的 Hello World 程序,其中“World”是我的 App 组件的参数,我的 server.js 和 browser.js 都使用共享的 App 组件来设置参数(在服务器中,参数值设置为“World”,并在浏览器中将参数值设置为“Me”)。 As I would expect, when I run the application using the hydrate function inside the browser.js it should ignores the difference because as I understand hydrate shouldn't change the component values but only add event listeners.正如我所料,当我使用 browser.js 中的 hydrate 函数运行应用程序时,它应该忽略差异,因为据我所知, hydr 不应该更改组件值而只添加事件侦听器。 But When I run the application I see Hello World and after few miliseconds it becomes Hello Me (which is what I would expect from render and not hydrate).但是当我运行应用程序时,我看到了 Hello World,几毫秒后它变成了 Hello Me(这是我对渲染而不是水合物的期望)。

Also I tried to test the new renderToNodeStream function by creating a sample app where the data from the server comes slowly.我还尝试通过创建一个示例应用程序来测试新的 renderToNodeStream 函数,其中来自服务器的数据传输缓慢。 I would expect my app to run while the server transfering chunks of data to the client but it seems to work exactly as renderToString (all the component string comes at once).我希望我的应用程序在服务器将数据块传输到客户端时运行,但它似乎与 renderToString 完全一样(所有组件字符串同时出现)。

Can please someone assist me and explain what I'm doing wrong?请有人帮助我并解释我做错了什么?

The performance won't have significant difference when the size of the component is small.当组件尺寸较小时,性能不会有显着差异。 The difference between renderToNodeStream and renderToString is renderToNodeStream will have renderer extended from stream.Readable之间的区别renderToNodeStreamrenderToStringrenderToNodeStream将有渲染器扩展stream.Readable

I think when the component's size getting huge(eg 10MB), renderToNodeStream will send piece by piece as the component parsed while renderToString will wait the parse finish then send the whole thing.我认为当组件的大小变得很大(例如 10MB)时, renderToNodeStream将在组件解析时renderToString块发送,而renderToString将等待解析完成然后发送整个内容。 I haven't did any test but I assume renderToNodeStream will have a better performance.我没有做过任何测试,但我认为renderToNodeStream会有更好的性能。

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

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