What are components in React? Create one class component “Car” in React and invoke it using index.js
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'));