内容简介:在react中,父组件的重新render会引发子组件的重新render,但是一些情况下我们会觉得这样做有些多余,比如:为了解决这个问题,需要分为ES6类组件和函数式组件两种:使用
在react中,父组件的重新render会引发子组件的重新render,但是一些情况下我们会觉得这样做有些多余,比如:
- 父组件并未传递props给子组件
- 新传递的props渲染结果不变
class A extends React.Component {
render() {
console.log('render')
return <div>这是A组件</div>
}
}
class Main extends React.Component {
render() {
return (
<div>
// 点击button会让A不断调用render
<button onClick={() => this.setState({ a: 1 })}>Main</button>
<A />
</div>
)
}
}
为了解决这个问题,需要分为ES6类组件和函数式组件两种:
类组件
使用 shouldComponentUpdate 来对props和state进行判断以此决定是否进行render
class A extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
//两次props对比
return nextProps.a === this.props.a ? false : true
}
render() {
console.log('render')
return <div>这是A组件</div>
}
}
class Main extends React.Component {
// ...
render() {
return (
<div>
<button onClick={() => this.setState({ a: 1 })}>Main</button>
<A a={this.state.a} />
</div>
)
}
}
通过返回false来跳过这次更新
使用 React.PureComponent ,它与 React.Component 区别在于它已经内置了 shouldComponentUpdate 来对props和state进行浅对比,并跳过更新
//PureComponent
class A extends React.PureComponent {
render() {
console.log('render')
return <div>这是A组件</div>
}
}
class Main extends React.Component {
state = {
a: 1
}
render() {
return (
<div>
<button onClick={() => this.setState({ a: 1 })}>Main</button>
<A a={this.state.a} />
</div>
)
}
}
函数组件
使用高阶组件 React.memo 来包裹函数式组件,它和类组件的PureComponent类似,也是对对props进行浅比较决定是否更新
const A = props => {
console.log('render A')
return <div>这是A组件</div>
}
// React.memo包裹A
const B = React.memo(A)
const Main = props => {
const [a, setA] = useState(1)
console.log('render Main')
return (
<div>
// 通过setA(a + 1)让父组件重新render
<button onClick={() => setA(a + 1)}>Main</button>
// 一直传入相同的props不会让子组件重新render
<B a={1} />
</div>
)
}
它的第二个参数接受一个两次props作为参数的函数,返回true则禁止子组件更新
其他
上面提到的浅比较就是根据内存地址判断是否相同:
// extends React.Component
class A extends React.Component {
render() {
console.log('render A')
console.log(this.props)
return <div>这是组件A</div>
}
}
class Main extends React.Component {
test = [1, 2, 3]
render() {
console.log('render Main')
return (
<div>
<button
onClick={() => {
// 父组件render
this.setState({})
this.test.push(4)
}}
>
Main
</button>
<A test={this.test} />
</div>
)
}
}
结果是:
使用React.component:
使用React.PureComponent:
使用React.component,点击之后子组件重新render。改为React.PureComponent之后,点击button子组件并不会render。也因此,PureComponent根据前后内存地址判断是否相等,所以向子组件传递函数作为props时,使用内联箭头函数的形式将会导致子组件的重新render;所以可以用箭头函数作为成员变量的形式再将函数引用作为props传递。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- React 组件模式-有状态组件 x 无状态组件、容器组件 x 展示组件、高阶组件 x 渲染回调(函数作为子组件)
- Serverless 组件开发尝试:全局变量组件和单独部署组件
- angular自定义组件-UI组件篇-switch组件
- React Hooks 源码解析(一):类组件、函数组件、纯组件
- Vue动态组件和异步组件
- Vue 动态组件 & 异步组件原理
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python 3学习笔记(上卷)
雨痕 / 电子工业出版社 / 2018-1 / 89
经过9 年的发展,Python 3 生态已相当成熟。无论是语言进化、解释器性能提升,还是第三方支持,都是如此。随着Python 2.7 EOF 日趋临近,迁移到Python 3 的各种障碍也被逐一剔除。是时候在新环境下学习或工作了。 人们常说Python 简单易学,但这是以封装和隐藏复杂体系为代价的。仅阅读语言规范很难深入,亦无从发挥其应有能力,易学难精才是常态。《Python 3学习笔记(......一起来看看 《Python 3学习笔记(上卷)》 这本书的介绍吧!