+1 vote
66 views
by (98.9k points)
What are components in React? Create one class component “Car” in React and invoke it using index.js

1 Answer

0 votes
by (98.9k points)
 
Best answer

In React, components are the building blocks that allow you to split the user interface into reusable and independent pieces. Components can be either functional or class-based. They encapsulate the logic and the UI elements related to a specific part of the application. 

 

Example : 

// Car.js

import React, { Component } from 'react';

class Car extends Component {
  render() {
    return (
      <div>
        <h2>Car Component</h2>
        <p>This is a simple car component.</p>
      </div>
    );
  }
}

export default Car;

 

// index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Car from './Car';

// Render the Car component in the root element of the HTML document
ReactDOM.render(<Car />, document.getElementById('root'));


Related questions

+1 vote
0 answers 151 views
0 votes
0 answers 29 views
+1 vote
1 answer 38 views
+1 vote
1 answer 155 views
0 votes
0 answers 20 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

504 users

...