码迷,mamicode.com
首页 > 其他好文 > 详细

[React Fundamentals] Component Lifecycle - Mounting Basics

时间:2016-08-16 01:58:44      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

React components have a lifecycle, and you are able to access specific phases of that lifecycle. This lesson will introduce mounting and unmounting of your React components.

 

import React from react;
import ReactDOM from react-dom;

export default class App extends React.Component {
    constructor(){
        super();
        this.state = {
            val: 0
        }
    }
    update(){
        this.setState({
            val: this.state.val + 1
        })
    }
    componentWillMount(){
        console.log("Component Will Mount");
    }
    render() {
        console.log("rendering");
        return (
            <div>
                <button onClick={this.update.bind(this)}>{this.state.val}</button>
            </div>
        )
    }
    componentDidMount(){
        console.log("Component Did Mount");
    }
}

 

"componentWillMount" happen before rendering, "state" and "props" are ready, but DOM is not rendered yet.

"componentDidMount" happen after component rendered to the DOM, can access DOM Node.

 

技术分享

[React Fundamentals] Component Lifecycle - Mounting Basics

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5774888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!