React——生命周期

一、先来一个简洁版的理解大纲
生命周期共提供了10个不同的API。
1.getDefaultProps
作用于组件类,只调用一次,返回对象用于设置默认的props,对于引用值,会在实例中共享。

2.getInitialState
作用于组件的实例,在实例创建时调用一次,用于初始化每个实例的state,此时可以访问this.props。

3.componentWillMount
在完成首次渲染之前调用,此时仍可以修改组件的state。

4.render
必选的方法,创建虚拟DOM,该方法具有特殊的规则:
* 只能通过this.props和this.state访问数据
* 可以返回null、false或任何React组件
* 只能出现一个顶级组件(不能返回数组)
* 不能改变组件的状态
* 不能修改DOM的输出

5.componentDidMount
真实的DOM被渲染出来后调用,在该方法中可通过this.getDOMNode()访问到真实的DOM元素。此时已可以使用其他类库来操作这个DOM。
在服务端中,该方法不会被调用。

6.componentWillReceiveProps
组件接收到新的props时调用,并将其作为参数nextProps使用,此时可以更改组件props及state。

   componentWillReceiveProps: function(nextProps) {
        if (nextProps.bool) {
            this.setState({
                bool: true
            });
        }
    }

7.shouldComponentUpdate
组件是否应当渲染新的props或state,返回false表示跳过后续的生命周期方法,通常不需要使用以避免出现bug。在出现应用的瓶颈时,可通过该方法进行适当的优化。在首次渲染期间或者调用了forceUpdate方法后,该方法不会被调用

8.componentWillUpdate
接收到新的props或者state后,进行渲染之前调用,此时不允许更新props或state。

9.componentDidUpdate
完成渲染新的props或者state后调用,此时可以访问到新的DOM元素。

10.componentWillUnmount
组件被移除之前被调用,可以用于做一些清理工作,在componentDidMount方法中添加的所有任务都需要在该方法中撤销,比如创建的定时器或添加的事件监听器。

二、基本上所有React组件生命周期方法可以分为下面三个阶段(详细版):
一、挂载(Mounting): 这个阶段发生在组件被创建并被插入到DOM时
constructor(props)
在React组件被挂载之前,构造器constructor()首先被调用,由于继承了React.Component,我们必须在其他语句之前首先调用super(props);否则,在构造器中this.props的值为undefined,从而引发错误

扫描二维码关注公众号,回复: 2465403 查看本文章

componentWillMount()
在组件挂载之前,componentWillMount()方法将会被调用。componentWillMount()方法在render()方法之前被调用,所以在该方法中设置状态不会触发重新渲染。

render()
render()方法是必须的,并返回null、false 或者一个React Element。 当返回 null 或者 false 时,表示你不想渲染任何东西;并且ReactDOM.findDOMNode(this)将会返回null。 render()应该是纯粹的(pure),它不应该修改组件的状态,每次调用 都会返回相同的结果,它不直接与浏览器交互。如果我们想与浏览器进行交互,我们应该在componentDidMount()方法中完成相应的功能。

componentDidMount()
componentDidMount()将会在组件挂载之后被调用,在该方法中修改state将会触发重新渲染。可以在该方法中加载远程数据,然后根据新的数据重新渲染DOM。该方法不会在服务端被渲染过程中调用。该方法被调用时,已经渲染出真实的 DOM,可以再该方法中通过this.getDOMNode()访问到真实的 DOM(推荐使用 ReactDOM.findDOMNode())。

二、更新(Updating): 这个阶段发生在组件被重新渲染成虚拟DOM并决定实际DOM是否需要更新时

componentWillReceiveProps(nextProps)
组件的props可以通过父辈组件来更改, componentWillReceiveProps()会在一个被挂载的组件接收到新属性的时候被调用,有的时候尽管没有属性发生改变,React 依然会调用componentWillReceiveProps(),所以如果我们只想处理属性改变的情况,我们需要对比 this.props 和 nextProps 。 在初始挂载的时候componentWillReceiveProps不会被调用,调用this.setState通常不会触发componentWillReceiveProps。

shouldComponentUpdate(nextProps, nextState)
默认情况下,在状态发生改变的时候,组件会重新渲染,在渲染之前,shouldComponentUpdate()将会被调用,默认返回true。在组件初始渲染或者调用forceUpdate()时,该方法不会被调用。 如果shouldComponentUpdate()返回false,那么componentWillUpdate()、render()、componentDidUpdate()将不会被调用,但是返回false不会影响子组件的重新渲染。

componentWillUpdate(nextProps, nextState)
在渲染之前,componentWillUpdate()将会被调用,组件第一次渲染之前不会调用该方法。 注意,我们不能在这里调用this.setState(),如果我们需要更新状态,我们应该在componentWillReceiveProps()中进行更新。

componentDidUpdate(prevProps, prevState)
更新完成之后,componentDidUpdate()会被立即调用,组件第一次渲染完成之后不会调用该方法。

三、卸载(Unmounting): 这个阶段发生在组件从DOM中被删除时
componentWillUnmount()
在组件卸载之前,componentWillUnmount()方法会被调用
这里写图片描述

三、面试题setState not working in componentDidMount() Why?

The component does not listen to state modifications in didMount; that means that even if the state is updated the repaint is not triggered. a re-rendering
初始化-render-Did,不会再重复渲染

参考资料:https://github.com/Marco2333/react-demo/tree/master/demo/demo03%20%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F

猜你喜欢

转载自blog.csdn.net/michellezhai/article/details/80098075