components in reactjs

  components in react js:


 In simple words, a component in React.js is like a block that you use to build your user interface. It's a small, reusable piece of your website or web application. Each component does a specific job, like showing a button, displaying a message, or managing a part of the page. By putting these components together, you create a complete and interactive user interface. Components help keep your code organized, make it easier to understand, and allow you to build and reuse different parts of your website or app.

In React.js, components are the building blocks of a user interface. They are reusable, self-contained pieces of code that can be composed together to create complex UIs. Components can be classified into two main types: functional components and class components.

Functional Components:

Functional components are simpler and commonly used when the component doesn't need to manage state or lifecycle methods. They are defined as JavaScript functions that take props as an argument and return React elements.


Example of a functional component:

app.js file:

import logo from './logo.svg';
import './App.css';
import Greet from './Components/Greet';
function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
        <h1>functional component :</h1>
        <Greet/>
      </header>
    </div>
  );
}

export default App;

Create one folder in src name: Component and create one file- Greet:

Greet.js:

export default function Greet()
{
    return <h2>from functional component</h2>
}

Class Components:

Class components are used when you need to manage state, lifecycle methods, or use other features that functional components don't provide. They are defined using ES6 classes and extend the React.Component class.

Example of a class component:

app.js:

import logo from './logo.svg';
import './App.css';
import Greet from './Components/Greet';
import Welcome from './Components/Welcome';
function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
        <h1>functional component :</h1>
        <Greet/>

        <h1> Class component:</h1>
        <Welcome/>
      </header>
    </div>
  );
}

export default App;

Welcome.js:

import {Component} from "react";
class Welcome extends Component
{
    render()
    {
        return <h2>from welcome class component</h2>
    }
}
export default Welcome



Post a Comment

Previous Post Next Post