React生命周期详解

作者: 暮落晨曦 | 来源:发表于2017-09-03 00:09 被阅读68次

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,本文禁止转载。

相关文章

  • React 生命周期详解

    React 生命周期详解 生命周期的三个阶段 装载过程: Mouth React组件的第一次渲染DOM的过程, 更...

  • react组件生命周期

    下图详细列述了 React 组件在整个生命周期中所涉及的方法和行为: 生命周期详解 组件在整个 ReactJS 的...

  • vue生命周期

    vue生命周期详: vue生命周期详解图: vue生命周期详解代码展示:

  • React 经典案例--TodoList

    上次写了一个React生命周期详解,给新手看还是不是特别容易理解(其实我也是新手),这边再做一个React的tod...

  • React概念图

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

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

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

  • React生命周期详解

    Mounting / 挂载阶段 getDefaultProps->getInitialState->compone...

  • react生命周期详解

    这一部分内容一直一知半解,最近发现一篇文章,非常好的解释了生命周期的问题,留存在这里,以备后查! 简介 一个rea...

  • React生命周期详解

    1 React生命周期流程 调用流程可以参看上图。分为实例化,存在期和销毁三个不同阶段。介绍生命周期流程的文章很多...

  • react生命周期详解

    上面的这幅图已经很好地诠释一个react组件的生命周期,所以认真看图!认真看图!认真看图! 一、getDefaul...

网友评论

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

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