YogeshChauhan . com

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.

dreamhost

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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 5 Use inline if to make a shorter conditional syntax in React May 4 What is Conditional Rendering in React? May 3 How does @extend rule work in SCSS (Sass)? May 2 How different is Handling Events in React vs HTML DOM? May 2 How to create bouncing balls using HTML canvas and JavaScript? Apr 30 HTML canvas methods

You might also like these

How to create Glowing Text using CSS?CSSThe difference between isFinite() method and isFinite() function in JavaScriptJavaScriptIntroduction to components and templates Part 2: Templates and viewsAngularHow to convert an object from API to JSON array in Angular 9?AngularPostgreSQL transactions using the BEGIN, COMMIT, and ROLLBACK statements.PostgresHow to create two segues with two UIButtons on a single page (Swift 5.0)?Swift