ReactJS - Simple Example for state and event handling

State management in ReactJS is handled in a way that is anything change occurs in state, DOM gets updated automatically. And this will help you write no code for rendering html element after data is gets changed.

 

import React, { Component } from 'react';

export default class EventDemo extends Component {
    
    constructor(props){
      super(props);
      this.state={number:0};
    }
   
    checkThis=()=>{
      this.setState(state=>({
        number: state.number+1
      }));
    }

  render() {
    return (
        <div>
            <button onClick={this.checkThis}>{this.state.number}+1</button>
       </div>
    );
  }
}

 

reactjs state management image

Tags