React的新特性之memo

实现指定组件进行渲染

  • 使用shouldComponentUpdate实现指定组件渲染
import React, { Component} from 'react';
class Foo  extends Component{
  // 判断该组件是否重新渲染
  // 通过生命周期函数可以去判断,也可以通过PureComponent实现
  shouldComponentUpdate(nextProps,nextState){
    if(nextProps.name===this.props.name){
      return false;
    }
    return true
  }
  render(){
    console.log('Foo render')
  return <div>{this.props.person.age}</div>;
  }
}
class App extends Component {
  state = {
    count: 0,
    person: {
      age: 1
    }
  }
  callback = () => {

  }
  // 当count发生变化的时候,Foo组件会被重新渲染
  render() {
    const { person } = this.state
    return (
      <div>
        {/* <button onClick={()=>this.setState({
          count:this.state.count+1
        })}>add</button> */}
        <button onClick={() => {
          person.age++;
          this.setState({ count: this.state.count + 1 })
        }}>
          add
        </button>
        {/* 当我们给Foo传入一个内联函数的时候,即使person.age没有发生改变,也是会重新渲染 */}
        {/* <Foo person={person} cb={()=>{}} /> */}
        {/* 把传入的内联函数写成回调函数,就可以解决,就不会重新渲染 */}
        <Foo person={person} cb={this.callback} />
      </div>
    )
  }
}
export default App;
  • 使用PureComponent实现指定组件渲染
import React, { Component, PureComponent, memo } from 'react';
// PureComponent在状态中的内容相对复杂的时候
// 可能就会不起作用,state中的第一级内容发生改变时,才会变化
// 而我们修改的是person对象中的age,会认为状态并没有发生改变,不会重新渲染
class Foo  extends PureComponent{
  render(){
    console.log('Foo render')
  return <div>{this.props.person.age}</div>;
  }
}
class App extends Component {
  state = {
    count: 0,
    person: {
      age: 1
    }
  }
  callback = () => {

  }
  // 当count发生变化的时候,Foo组件会被重新渲染
  render() {
    const { person } = this.state
    return (
      <div>
        {/* <button onClick={()=>this.setState({
          count:this.state.count+1
        })}>add</button> */}
        <button onClick={() => {
          person.age++;
          this.setState({ count: this.state.count + 1 })
        }}>
          add
        </button>
        {/* 当我们给Foo传入一个内联函数的时候,即使person.age没有发生改变,也是会重新渲染 */}
        {/* <Foo person={person} cb={()=>{}} /> */}
        {/* 把传入的内联函数写成回调函数,就可以解决,就不会重新渲染 */}
        <Foo person={person} cb={this.callback} />
      </div>
    )
  }
}
export default App;
  • 使用memo实现指定组件渲染
import React, { Component,  memo } from 'react';
const Foo = memo(function Foo(props) {
  console.log('Foo render')
  return <div>{props.person.age}</div>;
})
class App extends Component {
  state = {
    count: 0,
    person: {
      age: 1
    }
  }
  callback = () => {}
  // 当count发生变化的时候,Foo组件会被重新渲染
  render() {
    const { person } = this.state
    return (
      <div>
        {/* <button onClick={()=>this.setState({
          count:this.state.count+1
        })}>add</button> */}
        <button onClick={() => {
          person.age++;
          this.setState({ count: this.state.count + 1 })
        }}>
          add
        </button>
        {/* 当我们给Foo传入一个内联函数的时候,即使person.age没有发生改变,也是会重新渲染 */}
        {/* <Foo person={person} cb={()=>{}} /> */}
        {/* 把传入的内联函数写成回调函数,就可以解决,就不会重新渲染 */}
        <Foo person={person} cb={this.callback} />
      </div>
    )
  }
}
export default App;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值