[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(){
        super();
        this.state = {
            increasing: false
        }
    }
    update(){
        ReactDOM.render(<App val={this.props.val+1} /> , document.getElementById(‘app‘));
    }
    componentWillReceiveProps(nextProps){
        //Invoked when a component is receiving new props. This method is not called for the initial render.
        this.setState({
            increasing: nextProps.val > this.props.val
        })
    }
    shouldComponentUpdate(nextProps, nextState){
        // Control whether the component should re-render
        return nextProps.val % 5 === 0; // update dom every 5 clicks
    }
    render() {
        console.log(this.state.increasing);
        return (
            <div>
                <button onClick={this.update.bind(this)}>{this.props.val}</button>
            </div>
        )
    }
    componentDidUpdate(prevProps, prevState){
        //After DOM update
        console.log("prevProps", prevProps);
    }
}

App.defaultProps = {
    val: 0
}
时间: 2024-10-14 00:28:59

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

[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 - 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.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 (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 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 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