React生命周期详解

Each component has several "lifecycle methods" that you can override to run code at particular times in the process. Methods prefixed with will are called right before something happens, and methods prefixed with did are called right after something happens. --- React官方文档

挂载

初始化过程如下:

  • constructor
  • componentWillMount
  • render
  • componentDidMount

更新

当父组件给子组件传值时,会触发如下更新过程:

  • componentWillReceiveProps
  • shouldComponentUpdate
  • componentWillUpdate
  • render
  • componentDidUpdate

卸载

当一个组件被从DOM中移除时,该方法别调用:

  • componentWillUnmount

具体加载过程参考如下图:


具体代码

  1. 挂载
        class CycleLife extends React.Component {
            // 挂载过程
            // 1.构造器函数
            constructor(props) {
                super(props);
                console.log('构造器函数');
            }
            // 2.组件将要挂载
            componentWillMount() {
                console.log('组件将要被挂载');
            }
            // 3.组件渲染
            render() {
                console.log('组件被渲染');
                return (
                    <h1>Hello, World</h1>
                );
            }
            // 4.组件已经挂载
            componentDidMount() {
                console.log('组件已经被挂载');
            }
        }
    
  2. state 变更触发更新
        class CycleLife extends React.Component {
            constructor(props) {
                super(props)
                this.state = {
                    time: new Date()
                }
            }
            changeTime = () => {
                this.setState({
                    time: new Date()
                })
            }
            componentDidMount() {
                // 每隔一秒修改时间, 实现时钟效果
                setInterval(this.changeTime, 1000)
            }
            // 1. 判断是否需要组件更新, 默认 true
            shouldComponentUpdate(nextProps, nextState) {
                // 返回 false 时, 后续函数不执行
                return true
            }
            // 2. 组件将要更新
            componentWillUpdate(nextProps, nextState) {
                console.log('nextProps: ', nextProps)
                console.log('nextState: ', nextState)
                console.log('组件将要被挂载')
            }
            // 3. 组件被重新渲染
            render() {
                console.log('组件被重新渲染')
                return (
                    <h1>{this.state.time.toLocaleString()}</h1>
                );
            }
            // 4. 组件已经更新
            componentDidUpdate() {
                console.log('组件已经被挂载')
            }
        }
        ReactDOM.render(
            <CycleLife/>,
            document.getElementById('root')
        );
    
  3. 父 -> 子 传递属性时, 发生的生命周期
        class CycleLife extends React.Component {
            static propTypes = {
                content: React.PropTypes.string
            }
            constructor(props) {
                super(props)
            }
            // 1. 父 -> 子传递props时, 触发
            componentWillReceiveProps(nextProps, nextState) {
                console.log('组件属性被父级修改')
                console.log('nextProps: ', nextProps)
                console.log('nextState: ', nextState)
            }
            // 2. 判断是否需要组件更新, 默认 true
            shouldComponentUpdate(nextProps, nextState) {
                console.log('判断组件是否需要更新')
                // 返回 false 时, 后续函数不执行
                return true
            }
            // 3. 组件将要更新
            componentWillUpdate(nextProps, nextState) {
                console.log('nextProps: ', nextProps)
                console.log('nextState: ', nextState)
                console.log('组件将要被挂载')
            }
            // 4. 组件被重新渲染
            render() {
                console.log('组件被重新渲染')
                return (
                    <h1>{this.props.content}</h1>
                );
            }
            // 5. 组件已经更新
            componentDidUpdate() {
                console.log('组件已经被挂载')
            }
        }
        class FatherComponent extends React.Component {
            constructor(props) {
                super(props)
                this.state = {
                    content: '生命周期展示'
                }
            }
            componentDidMount() {
                this.setState({
                    content: '生命周期展示被改变'
                })
            }
            render() {
                return (
                    <CycleLife content={this.state.content}/>
                )
            }
        }
        ReactDOM.render(
            <FatherComponent />,
            document.getElementById('root')
        );
    

文章首发 learning-react,本文禁止转载。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这一部分内容一直一知半解,最近发现一篇文章,非常好的解释了生命周期的问题,留存在这里,以备后查! 简介 一个rea...
    春木橙云阅读 944评论 0 5
  • 上面的这幅图已经很好地诠释一个react组件的生命周期,所以认真看图!认真看图!认真看图! 一、getDefaul...
    好大一颗星阅读 303评论 0 2
  • 深入JSX date:20170412笔记原文其实JSX是React.createElement(componen...
    gaoer1938阅读 8,104评论 2 35
  • 1 React生命周期流程 调用流程可以参看上图。分为实例化,存在期和销毁三个不同阶段。介绍生命周期流程的文章很多...
    Dabao123阅读 330评论 0 1
  • 作为一个轻量级前端组件框架,react流行起来也不无道理,它可以不同程度的适配到你的应用中。你既可以只是轻量的引入...
    大路无疆阅读 970评论 0 8