调用setState後未重新執行 render?

情況一

shouldComponentUpdate(object nextProps,object nextState),每个组件都有这个方法,它负责确定“应该组件更新(运行渲染函数)?” 每次更改状态或从父组件传递新的道具时

您可以为组件编写自己的shouldComponentUpdate方法实现,但默认实现始终返回true —— 意味着始终重新运行render函数。

从官方文档引用http://facebook.github.io/react/docs/component-specs.html#updating-shouldcomponentupdate

默认情况下,shouldComponentUpdate始终返回true以防止在状态发生变异时出现细微错误,但是如果您小心始终将状态视为不可变并且只读取render()中的props和state,那么您可以使用实现覆盖shouldComponentUpdate将旧的道具和州与他们的替代品进行比较。

 情況二

PureComponent的扩展,它已经在shouldComponentUpdate内部实现了一个方法,用于浅层比较。

发布了91 篇原创文章 · 获赞 76 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/u013291076/article/details/98620232