Dung (Donny) Nguyen

Senior Software Engineer

React Component

A React component is one of the fundamental building blocks of a React application. Components encapsulate part of the user interface (UI) and can be reused across the application. There are two main types of React components: functional components and class components.

Types of React Components

Functional Components

Class Components

Key Concepts

Component Lifecycle

Example Usage

Combining functional and class components:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

class App extends React.Component {
  render() {
    return (
      <div>
        <h1>Welcome to my app</h1>
        <Counter />
      </div>
    );
  }
}

export default App;

In this example, the Counter functional component uses the useState hook to manage state, and the App class component renders the Counter component.

React components are powerful tools for building dynamic, reusable, and maintainable UIs.