React16生命周期

作者: fenerchen | 来源:发表于2019-08-06 11:53 被阅读3次

react生命周期,先上官网的图


react16生命周期.png

关于各生命周期函数的参数、返回值以及何时执行,啥也不说了,上代码,自己跑一边就啥都明白了。

//组件

class Testlifecycles extends React.Component {
  constructor(props) {
    super(props);
    console.log('constructor');
    this.handleClick = this.handleClick.bind(this);
    this.state = {b: 1};
    // getDefaultProps:接收初始props
    // getInitialState:初始化state
  }
  static getDerivedStateFromProps(props, state) {
    console.log('getDerivedStateFromProps', props, state);

    // 组件每次被rerender的时候,包括在组件构建之后(虚拟dom之后,实际dom挂载之前),每次获取新的props或state之后;;每次接收新的props之后都会返回一个对象作为新的state,返回null则说明不需要更新state
    return state;
  }
  componentDidCatch(error, info) {
    console.log('componentDidCatch', error, info);
    // 获取到javascript错误
  }
  handleClick() {
    this.props.parentClick();
  }
  render() {
    return <h2 onClick={this.handleClick}>点击试一下,a:{this.props.a}</h2>;
  }
  componentDidMount() {
    console.log('componentDidMount');
    // 挂载后
  }
  shouldComponentUpdate(nextProps, nextState) {
    console.log('shouldComponentUpdate', nextProps, nextState);

    // 组件Props或者state改变时触发,true:更新,false:不更新
    return true;
  }
  getSnapshotBeforeUpdate(prevProps, prevState) {
    console.log('getSnapshotBeforeUpdate', prevProps, prevState);
    // 组件更新前触发
    return null;
  }
  componentDidUpdate() {
    console.log('componentDidUpdate');
    // 组件更新后触发
  }
  componentWillUnmount() {
    console.log('componentWillUnmount');
    // 组件卸载时触发
  }
}

//在App中调用Testlifecycles组件
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {a: 1};
  }
  parentClick() {
    this.setState({a: 2});
  }

  render() {
    let x;
    if (this.state.a === 1) {
      x = (
        <Testlifecycles
          parentClick={() => this.parentClick()}
          a={this.state.a}
        />
      );
    } else {
      x = <div>estlifecycles组件被卸载</div>;
    }
    return <div>{x}</div>;
  }
}
//将App挂载到根节点下
ReactDOM.render(<App />, document.getElementById('root'));

参考资料:
生命周期图谱
React.Component
React-新的生命周期(React16版本)

相关文章

  • ReactV16.3+新增的api

    React16新的生命周期弃用了componentWillMount、componentWillReceivePo...

  • react的生命周期钩子@郝晨光

    本人之前写在CSDN上的文章,原创转载至此;react的生命周期钩子 1、React16新的生命周期弃用了comp...

  • React生命周期及减少render次数

    React生命周期 环境:react16 常用生命周期的钩子分类后,分布3个阶段内 初始化阶段 - 组件创建阶段才...

  • 浅谈React Fiber

    背景 前段时间准备前端招聘事项,复习前端React相关知识;复习React16新的生命周期:弃用了componen...

  • 解读 React Fiber

    背景 前段时间准备前端招聘事项,复习前端React相关知识;复习React16新的生命周期:弃用了componen...

  • React中类组件与函数组件

    类组件 React 16.8+的生命周期分为三个阶段,分别是挂载阶段、更新阶段、卸载阶段。 react16之后有三...

  • react16新特性

    前言 这里将列出react16所有新特性。我觉得重要的:1、render 支持返回数组和字符串2、新生命周期get...

  • ReactDom.render 源码阅读

    React16 源码简介 React16 重写了核心算法 reconciler。因为 JavaScript 引擎线...

  • React16生命周期

    react生命周期,先上官网的图 关于各生命周期函数的参数、返回值以及何时执行,啥也不说了,上代码,自己跑一边就啥...

  • fiber Eng

    Before react16 introduced the Fiber framework, React uses...

网友评论

    本文标题:React16生命周期

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