Yogesh Chauhan's Blog

How to add Local State to a Class in React?

in React on April 26, 2021

First of all, let’s understand why we need to do that.

We saw earlier in this Components and Props in React post that you can declare the components in two different ways. No matter which method you use to do so, one thing is clear. It must not modify its props.

That’s where the states concept comes into play.

So, we learned How to convert a function component into a class in React?

Now, let’s modify that class and add a local state method.

This is our class without local state method.



class Clock extends React.Component {
  render(){
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.props.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}


Step 1: Replace this.props.date with this.state.date.



class Clock extends React.Component {
  render(){
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}


Step 2: Add a constructor.

We still need to pass the value for date. For that we’ll add a constructor inside the class with initial props this.state.



class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}



As you have noticed in the code above, we are passing the props to constructor now. Before, we’re passing that to a function since there was no class.

Class components should always call the base constructor with props.

Step 3: Remove props from the element.

If you’ve added the prop in the element to make the function component work, remove that now since we have a constructor to take the props.

Change this:



ReactDOM.render(
  <Clock date={new Date()} />,
  document.getElementById('root')
);


to this:



ReactDOM.render(
  <Clock />,
  document.getElementById('root')
);


That’s it. We just added a local state method to a class.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to detect if browser supports WebP format on server side PHP script?PHPWordPress: How to loop through ACF group fields?WordPressHow to hide and show menu on scroll using JavaScript and CSS?CSSHow to set up the local environment and workspace for Angular development?AngularData Flows in ReactReactLearn to create profile card using HTML and CSSCSS