日日行,不怕千万里

React_PureComponent 简化 react shouldComponentUpdate 方法,优化性能

PureComponent  与  Component  类似,自react  15.3 版本之后使用,主要为了提高组件的重复加载问题,提高性能,类似于 shouldComponentUpdate功能。

 

import React, { Component, PureCompoent }  from 'react'

class A extends PureComponent {

  constructor(props) {

    super(props);

  } 

  render() {

    console.log("A组件加载")

    return (

      <div>{this.props.data}</div>

    )

  }

}

 

class B extends PureComponent {

  constructor(props) {

    super(props);

  } 

  render() {

    console.log("B组件加载")

    return (

      <div>{this.props.data}</div>

    )

  }

}

 

export   default   class Compoents_1 extends PureComponent {

  constructor(props) {

    super(props);

    this.state={data: 0}

  } 

  render() {

    console.log("B组件加载")

    return (

      <div>

        <A data={this.state.data}></A>

        <B data={this.state.data}></B>

        <button onClick={() => this.setState({data: 2})}></button>

      </div>

    )

  }

}

posted @ 2018-10-11 16:19  GongXiaoZhu  阅读(468)  评论(0编辑  收藏  举报