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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
The flex-grow, flex-shrink and flex-basis Properties in CSSCSSColumn and Table Alias in PostgresPostgresHow to zoom an image inside a box on hover?CSSAdvanced nesting in Sass (SCSS) using at-rootSCSSWhat is Hadoop and Hadoop Ecosystem?MiscellaneousLearn to create profile card using HTML and CSSCSS