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.


amazon

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 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS? #Aug 1 What is Zdog? #Aug 1 How to add before after image effect using pure CSS? #Jul 31 Add animation to your skills bar using CSS #Jul 31 Use SwiperJS to create mobile touch sliders fast
You might also like these
How to remove trailing characters from a string using JavaScript?JavaScriptHow to create a placeholder loader (throbber) using CSS?CSSContent Blocks in SCSS (Sass)SCSSHow to add today’s date in HTML input date value using JavaScript?JavaScriptUse eq() method in jQueryjQueryHow to Make CSS Lists Bullets Smaller?CSS