Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

reactjs lifecycle class components

import React from 'react';
import ReactDOM from 'react-dom';
 
class Test extends React.Component {
    constructor(props)
    {
        super(props);
        this.state = { hello : "World!" };
    }
 
    componentWillMount()
    {
        console.log("componentWillMount()");
    }
 
    componentDidMount()
    {
        console.log("componentDidMount()");
    }
 
    changeState()
    {
        this.setState({ hello : "Geek!" });
    }
 
    render()
    {
        return (
            <div>
            <h1>GeeksForGeeks.org, Hello{ this.state.hello }</h1>
            <h2>
            <a onClick={this.changeState.bind(this)}>Press Here!</a>
            </h2>
            </div>);
    }
 
    shouldComponentUpdate(nextProps, nextState)
    {
        console.log("shouldComponentUpdate()");
        return true;
    }
 
    componentWillUpdate()
    {
        console.log("componentWillUpdate()");
    }
 
    componentDidUpdate()
    {
        console.log("componentDidUpdate()");
    }
}
 
ReactDOM.render(
    <Test />,
    document.getElementById('root'));
Comment

Implementing state lifecycle in react class component

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  componentDidMount() {
    document.title = `You clicked ${this.state.count} times`;
  }
  componentDidUpdate() {
    document.title = `You clicked ${this.state.count} times`;
  }

  render() {
    return (
      <div>
        <p>You clicked {this.state.count} times</p>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>
          Click me
        </button>
      </div>
    );
  }
}
Comment

React Component Lifecycle

What is the order that Class components run when React initialize and mounts 
the components.

1. Regardless of React Components, constructors are always ran first 
	1. In constructor, you will just initialize the state in constructor
2. Render runs second, which determines what to show. This is the template 
   of the HTML and dictates the UI is going to be.
3. Third one that runs is componentDidMount, React runs the constructor, 
   runs the render and mounts the initial state and mounts on. Then it 
   will runs the code in componentDidMount();
   
- When setState() is called, the render() method will be called again and 
  re-renders.
	- Every subsequent child with a different props will also be re-rendered.
Comment

PREVIOUS NEXT
Code Example
Javascript :: check if all array elements are equal 
Javascript :: custom hook 
Javascript :: vuejs slots events 
Javascript :: array length 
Javascript :: javascript loading animation 
Javascript :: react-phone-number-input properties 
Javascript :: express ubuntu ERR_CONNECTION_REFUSED 
Javascript :: react native elements bottom sheet close on back button press 
Javascript :: javascript how do I measure the time of the loop 
Javascript :: Custom delay function for waitfor puppeteer 
Javascript :: javascript count occurence of character in string 
Javascript :: modal example react native 
Javascript :: hide console log level in js 
Javascript :: data-parsley-errors-container 
Javascript :: .tolowercase 
Javascript :: axios post data vue js 
Javascript :: form submit jquery 
Javascript :: how to use crypto module in nodejs 
Javascript :: how to get ip address and port from url in javascript 
Javascript :: jquery add event to dynamically created element 
Javascript :: javascript compare timestamp 
Javascript :: react native ios assessibility font size 
Javascript :: update object within object by id js 
Javascript :: how to update json key name while keeping the values in mysql 
Javascript :: setstate react 
Javascript :: escape xss javascript 
Javascript :: javascript how-do-i-check-whether-a-checkbox-is-checked-in-jquery 
Javascript :: update karma jasmine to specific version 
Javascript :: javascript sort array 
Javascript :: what is vue.js 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =