[React Fundamentals] Component Lifecycle - Mounting Basics

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.

时间: 2024-08-07 18:52:01

[React Fundamentals] Component Lifecycle - Mounting Basics的相关文章

[React Fundamentals] Component Lifecycle - Mounting Usage

The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson you will learn some simple uses for these hooks. import React from 'react'; import ReactDOM from 'react-dom'; class App extends React.Component { con

[React Fundamentals] Component Lifecycle - Updating

The React component lifecycle will allow you to update your components at runtime. This lesson will explore how to do that. import React from 'react'; import ReactDOM from 'react-dom'; export default class App extends React.Component { constructor(){

React.js Tutorial: React Component Lifecycle

Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of the following three statuses: mounted, update and unmounted. So React component lifecycle can be divided into three phases according to these statuse

react component lifecycle

react comments 状态: 1.Mounted:React Components 被render解析生成DOM节点并被插入浏览器DOM结构的一个过程 2.Updated:Mounted的React Components 被重新render的过程 3.Unmounted:一个Mounted的React Component对应的DOM节点被从DOM中移除的过程 每一个状态都对应封装了相应的hook函数 hook函数Mounting:componentWillMount:在Component

React (Native) Rendering Lifecycle

How Does React Native Work? The idea of writing mobile applications in JavaScript feels a little odd. How is it possible to use React in a mobile environment? In order to understand the technical underpinnings of React Native, first we’ll need to rec

React Native -- The Life-Cycle of a Composite Component

/** * ------------------ The Life-Cycle of a Composite Component ------------------ * * - constructor: Initialization of state. The instance is now retained. * - componentWillMount * - render * - [children's constructors] * - [children's componentWil

[React Fundamentals] State Basics

State is used for properties on a component that will change, versus static properties that are passed in. This lesson will introduce you to taking input within your React components. Unlike props, which are meant to be passed into our component as s

[React Fundamentals] Introduction to Properties

This lesson will teach you the basics of setting properties in your React components. class App extends React.Component { render(){ let txt = this.props.txt return <h1>{txt}</h1> } } App.propTypes = { txt: React.PropTypes.string, cat: React.Pr

[React Fundamentals] Using Refs to Access Components

When you are using React components you need to be able to access specific references to individual components. This is done by defining a ref. Notice: 'ref' only works in class component, not in statless component. If we don't add "ref", three