react笔记之组件的生命周期

责编:menVScode 2017-07-18 23:20 阅读(964)

        组件的生命周期,无非是组件的 "出生" 到 "死亡" 的过程。组件的生命周期大概会分为三个阶段:jsx渲染进页面(Mounting): 是组件第一次被渲染进页面的阶段;更新state(Updating):用setState更新组件内部状态阶段;组件被移除(Unmounting)。在组件的一生中Mounting和Unmounting只能被进行一次,Updating则代表组件丰富的一生,什么时候改边这个状态那个状态。每一个阶段都会进行某些动作,而每个动作执行之前或者执行之后,会去调用函数,我们把这些函数叫做生命周期函数。组件在被渲染页面之前和渲染页面之后,各自调用不同的函数;在更新前和更新后,各自调用不同的函数;在被移除之前,调用一个函数。所以了解组件的生命周期,无非是如何使用那些生命周期的函数,这些生命周期的函数,我们也称其为钩子函数。

The Component Lifecycle

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.

Mounting

These methods are called when an instance of a component is being created and inserted into the DOM:

        1、constructor() 

        2、componentWillMount() 

        3、render()
        4、componentDidMount() 

Updating

An update can be caused by changes to props or state. These methods are called when a component is being re-rendered:

        1、componentWillReceiveProps() 

        2、shouldComponentUpdate()
        3、componentWillUpdate() 
        4、render() 
        5、componentDidUpdate() 

Unmounting

This method is called when a component is being removed from the DOM:

        1、componentWillUnmount() 
前端交流群: MVC前端网(menvscode.com)-qq交流群:551903636

邮箱快速注册

忘记密码