dots Created with Sketch.
Yogesh Chauhan's Blog

How to convert a function component into a class in React?

in React on April 26, 2021

As we have already seen in the React Basics, components are small chunk of code like functions. They are reusable and helps to manage the UI easily.

We saw earlier in this Components and Props in React post that you can declare the components in two different ways.

When your app is small and it doesn’t require updating the UI, you’d want to go with a function component but as it grows bigger and require some UI modification, you’d want to go for a class component.

I am not saying that you can’t update the UI with a function component but with a function component, you need to render the element again and again to update the UI, which is not the way React suggests to update the UI. React suggests using states to update the UI and with a class component you can do that.

Let’s take a look at the function component first.



function Clock(props) {
  return (
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {props.date.toLocaleTimeString()}.</h2>
    </div>
  );
}


Let’s convert the above function component into a class component.

Step 1: Create an ES6 class.

You can use the same name for the class since in actual app/program, you might have used at multiple places. The class will extend React.Component to have all component’s properties and methods.



class Clock extends React.Component {
  ...
}


Step 2: Add a single method render() and keep it empty.



class Clock extends React.Component {
  render(){
    ...
  }
}


Step 3: Move the same body code inside the render() method.



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


Step 4: Replace the props with this.props.



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


That’s it. The Clock component is a class component now rather than a function component.

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

#Jul 22 Is there a CSS parent selector? #Jul 22 Difference between :where and :is in CSS #Jul 22 Does :is() pseudo selector hint at CSS preprocessing in the future? #Jul 22 Control Scrolling with CSS Scroll Snap #Jul 21 Control rendering using CSS content-visibility property #Jul 21 How to use @supports rule in CSS?
You might also like these
Solution for the error Commit failed – exit code 1 received in Desktop GithubMiscellaneous@forward rule in SCSS (Sass)SCSSWhat’s Interpolation in SCSS (Sass)?SCSSHow to Recognize an Array in JavaScript?JavaScriptHow to position an image on top of another image using CSS?CSSHow to use a Subquery to Insert Multiple Rows in SQL Table?SQL/MySQL