[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 {
    constructor(){
        super();
        this.state = {
            val: 0
        }
    }
    update(){
        this.setState({val: this.state.val + 1 })
    }
    componentWillMount(){
        console.log(this.state)
        this.setState({
            val: this.state.val * 2
        });
        console.log("Component Will Mount");
    }
    render() {
        console.log("rendering");
        return (
            <div>
                <button onClick={this.update.bind(this)}>{this.state.val}</button>
            </div>
        )
    }
    componentDidMount(){
        this.inc = setInterval(this.update.bind(this), 500);
        console.log("Component Did Mount");
    }
    componentWillUnmount(){
        clearInterval(this.inc);
        console.log("Component will unmount");
    }
}

export default class Wrapper extends React.Component{
    constructor(){
        super();
    }
    mount(){
        ReactDOM.render(<App />, document.getElementById(‘a‘));
    }
    unmount(){
        // Unmount a dom node
        ReactDOM.unmountComponentAtNode(document.getElementById(‘a‘))
    }
    render() {
        return (
            <div>
                <button onClick={this.mount.bind(this)}>Mount</button>
                <button onClick={this.unmount.bind(this)}>Unmount</button>
                <div id="a"></div>
            </div>
        );
    }

}
时间: 2024-10-13 20:05:28

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

[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

[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] 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

[React Fundamentals] Accessing Child Properties

When you're building your React components, you'll probably want to access child properties of the markup. In Angular, it is transcludion: <div> <ng-transculde></ng-transclude> </div> In React, it is: {this.props.children} It means

[React Fundamentals] Owner Ownee Relationship

The owner-ownee relationship is used to designate a parent-child relationship with React components as it differs from the DOM relationship. import React from 'react'; export default class App extends React.Component { constructor(){ super(); //This