+1 vote
645 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 620 views
0 votes
0 answers 293 views
+1 vote
1 answer 321 views
+1 vote
1 answer 709 views
0 votes
0 answers 238 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!

Recent Badges

Doubtly Earned a badge
Doubtly Earned a badge
Doubtly Earned a badge

5.7k questions

5.1k answers

108 comments

650 users

...