博客
关于我
react 简单优化设计-纯组件的优化
阅读量:166 次
发布时间:2019-02-28

本文共 621 字,大约阅读时间需要 2 分钟。

加入有如下代码;

setTimeout(()=>{            },1000)

这里每隔1 秒 就刷新一次,这个时候 A 组件会每次重复diff 算法,看是否该重新渲染,然后走render 方法

这样效率太低,我们的设想就是,只要 data 数据不变,我们就不会渲染,这样会节省很大的成本

所以这就有了纯组件的概念:一种是class 的方式(通过继承实现)

一种是 函数的方式,我们通过 函数包装的方式实现

PureComponent

定制了shouldComponentUpdate后的Component

class Comp extends React.PureComponent

React.memo

React 16.6.0 使用 React.memo 让函数式的组件也有PureComponent的功能

const Joke = React.memo(() => (
{this.props.value || 'loading...' }
)

只要通过这个两种方式包装后就会实现 显示组件的属性值的浅比较,这个时候

我们需要注意点,注意点,注意点::

就是给纯组件传递值的时候,一定要是 值传递!

data= {    a:100,    b:88}

// 这种方式不行,传递的是引用,指针,不是值传递

<A data= {data}></A>

应当如下方式传递:这种方式相当于 

这种写法见到了就行了!

转载地址:http://jtdc.baihongyu.com/

你可能感兴趣的文章
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>