1.所谓浅比较
(shallowEqual),即react源码中的一个函数,而后根据下面的方法进行是否是PureComponent
的判断,帮咱们作了原本应该咱们在shouldComponentUpdate
中作的事情javascript
if (this._compositeType === CompositeTypes.PureClass) {
shouldUpdate = !shallowEqual(prevProps, nextProps) || ! shallowEqual(inst.state, nextState);
}
复制代码
而原本咱们作的事情以下,这里判断了state
有没有发生变化(prop同理),从而决定要不要从新渲染,这里的函数在一个继承了Component
的组件中,而这里this.state.person
是一个对象,你会发现, 在这个对象的引用没有发生变化的时候是不会从新render
的(即下面提到的第三点),因此咱们能够用shouldComponentUpdate
进行优化,这个方法若是返回false
,表示不须要从新进行渲染,返回true
则从新渲染,默认返回true
java
shouldComponentUpdate(nextProps, nextState) {
return (nextState.person !== this.state.person);
}
复制代码
2.上面提到的某些状况下可使用PureComponent
来提高性能,那具体是哪些状况能够,哪些状况不能够呢,实践出真知react
3.以下显示的是一个IndexPage
组件,设置了一个state
是isShow
,经过一个按钮点击能够改变它的值,结果是:初始化的时候输出的是constructor
,render
, 而第一次点击按钮,会输出一次render,即从新渲染了一次,界面也会从显示false
变成显示true
,可是当这个组件是继承自PureComponent
的时候,再点击的时,不会再输出render
,即不会再从新渲染了, 而当这个组件是继承自Component
时,仍是会输出render
,仍是会从新渲染,这时候就是PureComponent
内部作了优化的体现数组
4.同理也适用于string
,number
等基本数据类型,由于基本数据类型,值改变了就算改变了函数
import React, { PureComponent } from 'react';
class IndexPage extends PureComponent{
constructor() {
super();
this.state = {
isShow: false
};
console.log('constructor');
}
changeState = () => {
this.setState({
isShow: true
})
};
render() {
console.log('render');
return (
<div> <button onClick={this.changeState}>点击</button> <div>{this.state.isShow.toString()}</div> </div>
);
}
}
复制代码
5.当这个this.state.arr
是一个数组时,且这个组件是继承自PureComponent
时,初始化依旧是输出constructor
和render
,可是当点击按钮时,界面上没有变化,也没有输出render
,证实没有渲染,可是咱们能够从下面的注释中看到,每点击 一次按钮,咱们想要修改的arr
的值已经改变,而这个值将去修改this.state.arr
,可是由于在PureComponent
中浅比较
这个数组的引用没有变化因此没有渲染,this.state.arr
也没有更新,由于在this.setState()
之后,值是在render
的时候更新的, 这里涉及到this.setState()
的知识性能
6.可是当这个组件是继承自Component
的时候,初始化依旧是输出constructor
和render
,可是当点击按钮时,界面上出现了变化,即咱们打印处理的arr
的值输出,并且每点击一次按钮都会输出一次render
,证实已经从新渲染,this.state.arr
的值已经更新,因此 咱们能在界面上看到这个变化优化
import React, { PureComponent } from 'react';
class IndexPage extends PureComponent{
constructor() {
super();
this.state = {
arr:['1']
};
console.log('constructor');
}
changeState = () => {
let { arr } = this.state;
arr.push('2');
console.log(arr);
// ["1", "2"]
// ["1", "2", "2"]
// ["1", "2", "2", "2"]
// ....
this.setState({
arr
})
};
render() {
console.log('render');
return (
<div> <button onClick={this.changeState}>点击</button> <div> {this.state.arr.map((item) => { return item; })} </div> </div>
);
}
}
复制代码
7.下面的例子用扩展运算符
产生新数组,使this.state.arr
的引用发生了变化,因此初始化的时候输出constructor
和render
后,每次点击按钮都会输出render
,界面也会变化,无论该组件是继承自Component
仍是PureComponent
的ui
import React, { PureComponent } from 'react';
class IndexPage extends PureComponent{
constructor() {
super();
this.state = {
arr:['1']
};
console.log('constructor');
}
changeState = () => {
let { arr } = this.state;
this.setState({
arr: [...arr, '2']
})
};
render() {
console.log('render');
return (
<div> <button onClick={this.changeState}>点击</button> <div> {this.state.arr.map((item) => { return item; })} </div> </div>
);
}
}
复制代码
8.上面的状况一样适用于对象
的状况this
1.咱们让IndexPage
组件里面包含一个子组件Example
来展现PureComponent
是如何影响子组件的spa
2.父组件继承PureComponent
,子组件继承Component
时:下面的结果初始化时输出为constructor
,IndexPage render
,example render
,可是当咱们点击按钮时,界面没有变化,由于这个this.state.person
对象的引用没有改变,只是改变了它里面的属性值 因此尽管子组件是继承Component
的也没有办法渲染,由于父组件是PureComponent
,父组件根本没有渲染,因此子组件也不会渲染
3.父组件继承PureComponent
,子组件继承PureComponent
时:由于渲染在父组件的时候就没有进行,至关于被拦截了,因此子组件是PureComponent
仍是Component
根本不会影响结果,界面依旧没有变化
4.父组件继承Component
,子组件继承PureComponent
时:结果和咱们预期的同样,即初始化是会输出constructor
,IndexPage render
,example render
,可是点击的时候只会出现IndexPage render
,由于父组件是Component
,因此父组件会渲染,可是 当父组件把值传给子组件的时候,由于子组件是PureComponent
,因此它会对prop
进行浅比较,发现这个person
对象的引用没有发生变化,因此不会从新渲染,而界面显示是由子组件显示的,因此界面也不会变化
5.父组件继承Component
,子组件继承Component
时:初始化是会输出constructor
,IndexPage render
,example render
,当咱们第一次点击按钮之后,界面发生变化,后面就再也不改变,由于咱们一直把它设置为sxt2,可是每点击一次 都会输出IndexPage render
,example render
,由于 每次无论父组件仍是子组件都会渲染
6.因此正以下面第四条说的,若是state
和prop
一直变化的话,仍是建议使用Component
,而且PureComponent
的最好做为展现组件
//父组件
import React, { PureComponent, Component } from 'react';
import Example from "../components/Example";
class IndexPage extends PureComponent{
constructor() {
super();
this.state = {
person: {
name: 'sxt'
}
};
console.log('constructor');
}
changeState = () => {
let { person } = this.state;
person.name = 'sxt2';
this.setState({
person
})
};
render() {
console.log('IndexPage render');
const { person } = this.state;
return (
<div> <button onClick={this.changeState}>点击</button> <Example person={person} /> </div> ); } } //子组件 import React, { Component } from 'react'; class Example extends Component { render() { console.log('example render'); const { person } = this.props; return( <div> {person.name} </div> ); } } 复制代码
1.继承自Component
的组件,如果shouldComponentUpdate
返回false
,就不会渲染了,继承自PureComponent
的组件不用咱们手动去判断prop
和state
,因此在PureComponent
中使用shouldComponentUpdate
会有以下警告:
IndexPage has a method called shouldComponentUpdate(). shouldComponentUpdate should not be used when extending React.PureComponent. Please extend React.Component if shouldComponentUpdate is used.
也是比较好理解的,就是不要在PureComponent
中使用shouldComponentUpdate
,由于根本没有必要