React常用生命周期

2021-04-15  本文已影响0人  走的是前方的路_端的是生活的苦

一、生命周期调用顺序

1. 挂载

2. 更新

3. 卸载

image.png

二、详解生命周期函数

1. construcor()

    constructor(props){
        super(props)
        // 不要在这里调用 this.setState()
        this.state = { counter: 0 };
        this.handleClick = this.handleClick.bind(this);
    }

2. componentDidMount()

3. render()

4. componentDidUpdate()

image.png
    componentDidUpdate(prevProps){
        //典型用法,判断props
        if(this.props.userId !== prevProps.userId){
            this.fetchData(this.props.userId)
        }
    }

5. componentWillUnmount()

上一篇 下一篇

猜你喜欢

热点阅读