V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ChefIsAwesome
V2EX  ›  React

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

  •  
  •   ChefIsAwesome · 2015-10-27 09:34:31 +08:00 · 2227 次点击
    这是一个创建于 3117 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

    1 条回复    2015-10-27 10:34:37 +08:00
    octref
        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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5959 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:29 · PVG 14:29 · LAX 23:29 · JFK 02:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.