加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 为什么React的PureComponent被推荐让它的所有孩子都

发布时间:2020-12-15 20:49:25 所属栏目:百科 来源:网络整理
导读:阅读官方 React documentation后,我遇到了关于PureComponent的 this: Furthermore,React.PureComponent’s shouldComponentUpdate() skips prop updates for the whole component subtree. Make sure all the children components are also “pure”. 为什
阅读官方 React documentation后,我遇到了关于PureComponent的 this:

Furthermore,React.PureComponent’s shouldComponentUpdate() skips prop
updates for the whole component subtree. Make sure all the children
components are also “pure”.

为什么跳过道具更新整个子树的确意味着避免使用非纯组件? PureComponent的组件子树中的非纯组件的后果是什么(一般情况下,以及在未设计/应该对props进行响应的情况下).

同一组输入道具的纯组件将给出完全相同的结果,不仅仅是为了它自己,而是为整个DOM树.当您声明PureComponent时,您不仅需要考虑道具和状态,还需要考虑上下文. PureComponents阻止任何上下文更改.考虑一个例子
<MyApp>      
 <Router>    // react-router.
  <App>   // A PureComponent
   <Switch>  // react-router Switch
     <Route ....>
   </Switch>
  </App>
 </Router>
</MyApp>

React-router的路由器将当前位置存储在上下文的路由器道具中.并且React-router的Switch会读回它并选择一个Route.But,因为App是一个非常纯粹的组件,并且不会对Router中的上下文更改作出反应,因为它不使用那些值而应该忽略它们.因此,当你有一个PureComponent时,你不仅应该考虑组件,还要考虑它的嵌套子组件.所以基本上你也会让你所有的孩子保持纯洁.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读