美文网首页
React生命周期

React生命周期

作者: 采姑娘的大白菜 | 来源:发表于2017-06-15 11:32 被阅读0次

组件的生命周期分成三个状态:

Mounting:已插入真实 DOM
Updating:正在被重新渲染
Unmounting:已移出真实 DOM

React 为每个状态都提供了两种处理函数,will函数在进入状态之前调用,did函数在进入状态之后调用,三种状态共计五种处理函数。

componentWillMount()
componentDidMount()
componentWillUpdate(object nextProps, object nextState)
componentDidUpdate(object prevProps, object prevState)
componentWillUnmount()

此外,React 还提供两种特殊状态的处理函数。

componentWillReceiveProps(object nextProps):已加载组件收到新的参数时调用
shouldComponentUpdate(object nextProps, object nextState):组件判断是否重新渲染时调用

下面是生命周期的例子,分别用React.Component和React.createClass呈现:

import React from 'react'
class lifecycle extends React.Component{ 
    //初始化组件状态getInitialState
    constructor(props) {
        super(props)
        this.state = {name: props.name}
        this.handleClick = this.handleClick.bind(this)
    }
    //初始渲染组件之前
    componentWillMount() {
        console.log("初始render之前 componentWillMount")
    }
    //初始渲染组件之后(初始render之后),通知组件已经加载完成
    componentDidMount() {
        console.log("初始render之后 componentDidMount")
    }
    //组件收到新的属性(props)
    componentWillReceiveProps(nextProps) {
        console.log("组件收到新的属性 componentWillReceiveProps")
    }
    //当组件接收到新的属性(props)和状态(state)改变的话,都会触发
    shouldComponentUpdate(nextProps, nextState) {
        console.log("组件接收到新的属性(props)和状态(state) shouldComponentUpdate")
        return true
    }
    //如果组件状态或者属性改变,并且上面的 shouldComponentUpdate(...) 返回为 true,就会开始准备更新组件,并调用 componentWillUpdate()
    componentWillUpdate() {
        console.log("准备更新组件 componentWillUpdate")
    }
    //调用了 render() 更新完成界面之后
    componentDidUpdate() {
        console.log("更新完成界面 componentDidUpdate")
    }
    //组件移除
    componentWillUnmount() {
        console.log("组件移除 componentWillUnmount")
    }
    handleClick() {
        console.log(this.state.name)
    }
    //渲染组件,每次数据更新并且上面的 shouldComponentUpdate(...) 返回为 true,都会调用渲染
    render() { 
        return ( 
            <div onClick={this.handleClick}></div> 
        )
    } 
}
lifecycle.propTypes =  {
    func: React.PropTypes.func,
    any: React.PropTypes.any,
    bool: React.PropTypes.bool,
    func: React.PropTypes.func,
    number: React.PropTypes.number,
    string: React.PropTypes.string,
    name : React.PropTypes.string.isRequired
}
//组件创建之前getDefaultProps
lifecycle.defaultProps = {
    name: 'wheat'
}
export default lifecycle


var lifecycle = React.createClass({
  propTypes: {
    func: React.PropTypes.func,
    any: React.PropTypes.any,
    bool: React.PropTypes.bool,
    func: React.PropTypes.func,
    number: React.PropTypes.number,
    string: React.PropTypes.string,
    name: React.PropTypes.string.isRequired
  },
  //组件创建之前
  getDefaultProps: function() {
    return {
      name: 'wheat'
    };
  },
  //初始化组件状态
  getInitialState : function(){
    console.log("初始化组件状态 getInitialState")
    return {
      name: this.props.name
    }
  },
  //初始渲染组件之前
  componentWillMount : function(){
      console.log("初始render之前 componentWillMount")
  },
  //初始渲染组件之后(初始render之后),通知组件已经加载完成
  componentDidMount : function(){
    console.log("初始render之后 componentDidMount")
  },
  //组件收到新的属性(props)
  componentWillReceiveProps : function(nextProps){
    console.log("组件收到新的属性 componentWillReceiveProps")
  },
  //当组件接收到新的属性(props)和状态(state)改变的话,都会触发
  shouldComponentUpdate : function(nextProps, nextState){
    console.log("组件接收到新的属性(props)和状态(state) shouldComponentUpdate")
    return true
  },
  //如果组件状态或者属性改变,并且上面的 shouldComponentUpdate(...) 返回为 true,就会开始准备更新组件,并调用 componentWillUpdate()
  componentWillUpdate : function(){
    console.log("准备更新组件 componentWillUpdate")
  },
  //调用了 render() 更新完成界面之后
  componentDidUpdate : function(){
      console.log("更新完成界面 componentDidUpdate")
  },
  //组件移除
  componentWillUnmount : function(){
    console.log("组件移除 componentWillUnmount")
  },
  handleClick: function() {
    console.log(this.state.name)
  },
  //渲染组件,每次数据更新并且上面的 shouldComponentUpdate(...) 返回为 true,都会调用渲染
  render: function() {
    console.log("渲染组件 render")
    return (
        <div onClick={this.handleClick}></div> 
    )
  }
})

参考资料:
react-without-es6
React Native 中组件的生命周期
React 入门实例教程

注:React Native组件的生命周期和React是一样的。

相关文章

  • React概念图

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

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

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

  • React生命周期

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

  • React v16 生命周期

    React 16 生命周期 React 16.3 新增的生命周期方法 逐渐废弃的生命周期方法: 一般将生命周期分成...

  • 学习并实现react (4)

    实现生命周期 生命周期介绍 React 生命周期图 React 子组件在父组件下的生命周期流程 实现 compon...

  • React面试题 整理脑图

    react基础 React生命周期 react-router react进阶 react Hooks redux 其他

  • react/vue常见问题整理

    一、react 1. react生命周期 react 16生命周期相对于15的变化:componentWillMo...

  • React 组件生命周期

    组件生命周期 参考阅读: component-lifecycle react组件生命周期过程说明 react 组件...

  • 《深入React技术栈》学习笔记Ⅲ

    以下的生命周期都是在 React 15 的生命周期, React 16 的生命周期 API 已经发生变化。Reac...

  • React总结

    [toc] 1.React组件生命周期 1.1 生命周期图 组件的生命周期的图如下: 具体可参考React 组件生...

网友评论

      本文标题:React生命周期

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