How to create a class component in React?

To create a class component in React, you can follow these steps:

  1. Create a new JavaScript file for your component (e.g. MyComponent.js).

  2. Import React at the top of the file:

    import React, { Component } from 'react';
  3. Create a new class that extends the Component class from React:

    class MyComponent extends Component { // Add your component logic here }
  4. Define a render() method inside your class component to return the JSX that will be rendered on the screen:

    class MyComponent extends Component { render() { return ( <div> <h1>Hello World!</h1> </div> ); } }
  5. Export your class component at the bottom of the file:

    export default MyComponent;
  6. Finally, you can import and use your class component in other files by importing it like this:

    import MyComponent from './MyComponent';

You have now successfully created a class component in React. You can add state, lifecycle methods, and other functionality to your component as needed.