在用 react 开发的时候,如果我们用 immutable data, virtual dom 是不是就不需要了

2015-10-27 09:34:31 +08:00
 ChefIsAwesome

react 现在的工作方式是这样的:
父元素的 state 改变 -> 所有子元素的 render 函数都会执行 -> virtual dom 检查所有的子元素,找到真正变化的子元素, render 它们

这样的机制下,“所有子元素的 render 函数都会执行”这一步无疑是影响性能的。所以 react 又提供了 shouldComponentUpdate 方法。如果我们用 immutable data ,配合 shouldComponentUpdate,我们开发者自己就能容易的找到真正变化的元素。这时候 virtual dom 就没有存在的必要了。

不需要 virtual dom ,那 jsx 也就不是必须的。事实上,如果我们尽量遵循最佳实践,写 stateless 的 component 。那大部分的 component 就只是没有什么逻辑的模板而已,跟其他以字符串为基础的模板库没什么不一样。

我们是否可以期待一个利用 immutable data 的新的前端框架?

2256 次点击
所在节点    React
1 条回复
octref
2015-10-27 10:34:37 +08:00
You should take a look at Redux[0] and Relay[1].

Basically all mutation and state updates should happen at highest level possible on the component hierarchy. That could be on the server or on the client depending on whether you need the server side.

Stateless components are still much superior than templates because of their high composability and fine-grain control of rendering.


[0]: https://github.com/rackt/redux
[1]: https://github.com/facebook/relay

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/231336

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX