美文网首页
图解React生命周期

图解React生命周期

作者: 独善wu身 | 来源:发表于2018-12-18 16:11 被阅读0次

图片来自:https://segmentfault.com/a/1190000016494335

1、如图所示,组件一开始会调用constructor函数,进行state以及props的初始化赋值。

2、随后进入componentWillCount,componentWillCount只在第一次render前调用。

3、在第一次render,即初始化render后,调用componentDidMount。

4、当运行期间,props发生变化时,调用componetWillReciveProps,在render前调用。

5、随后调用shouldComponentUpdate,该方法如果返回false,可以阻止组件渲染。通过条件判断,可以减少重复渲染。

6、之后,调用componentWillUpdate方法,通知即将开始渲染更新,在render前。

7、render完成后,调用componentDidUpdate方法,通知组件属性更新完成。

8、组件销毁,则调用componentWillUnMount,此方法中可以释放一些定时器,避免内存泄露。

示例代码:

实例代码来自:菜鸟教程React 组件生命周期

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>React 实例</title>

<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>

<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>

<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>

</head>

<body>

<div id="example"></div>

<script type="text/babel">

class Button extends React.Component{

  constructor(props) {

      super(props);

      this.state = {data: 0};

      this.setNewNumber = this.setNewNumber.bind(this);

  }

  setNewNumber() {

    this.setState({data: this.state.data + 1})

  }

  render() {

      return (

         <div>

            <button onClick = {this.setNewNumber}>INCREMENT</button>

            <Content myNumber = {this.state.data}></Content>

         </div>

      );

    }

}

class Content extends React.Component{

  componentWillMount() {

      alert('Component WILL MOUNT!')

  }

  componentDidMount() {

       alert('Component DID MOUNT!')

  }

  componentWillReceiveProps(newProps) {

        console.log('Component WILL RECEIVE PROPS!')

  }

  shouldComponentUpdate(newProps, newState) {

        return true;

  }

  componentWillUpdate(nextProps, nextState) {

          alert('Component WILL UPDATE!');

        console.log('Component WILL UPDATE!');

  }

  componentDidUpdate(prevProps, prevState) {

          alert('Component DID UPDATE');

        console.log('Component DID UPDATE!')

  }

  componentWillUnmount() {

          alert('Component WILL UNMOUNT!');

         console.log('Component WILL UNMOUNT!');

  }

    render() {

      return (

        <div>

          <h3>{this.props.myNumber}</h3>

        </div>

      );

    }

}

ReactDOM.render(

   <div>

      <Button />

   </div>,

  document.getElementById('example')

);

</script>

</body>

</html>

相关文章

  • React开发

    图解ES6中的React生命周期 React-PDF 帮你在 React 应用中展示 PDF 文件 Easily ...

  • react

    react简介 链接 静态编译,CoffeeScript jsx语法 react生命周期 链接 图解ES6中的...

  • React生命周期

    V16.3之前 图解 生命周期总览 react的生命周期大概分为 组件装载(Mount)组件第一次渲染到Dom树 ...

  • 图解React生命周期

    图片来自:https://segmentfault.com/a/1190000016494335 1、如图所示,组...

  • React概念图

    React概念图 React组件生命周期概念图 参考文档:React入门教程 组件生命周期React:组件生命周期...

  • React基础篇之组件的生命周期

    引出生命周期 react生命周期(旧) react生命周期(新) getSnapshotBeforeUpdate的...

  • react-native 生命周期

    (一)、图解 基本总结: 从图看:在React Native中,组件的生命周期大致可以分为3个阶段(实例化阶段,存...

  • [转]react-native 生命周期

    (一)、图解 基本总结: 从图看:在React Native中,组件的生命周期大致可以分为3个阶段(实例化阶段,存...

  • React生命周期的图解

    声明周期的概括图 生命周期的划分 初始化阶段 defaultProps 设置组件的默认属性 constructor...

  • React生命周期

    React v16.0前的生命周期 React v16.4+ 的生命周期图 React v16.9后这些生命周期钩...

网友评论

      本文标题:图解React生命周期

      本文链接:https://www.haomeiwen.com/subject/gfhjfxtx.html