ReasonJun

React : Why use React? (Component-Based Architecture) 본문

Frontend/React

React : Why use React? (Component-Based Architecture)

ReasonJun 2023. 6. 11. 00:03
728x90

Component-Based Architecture 

In React, Component-Based Architecture is a fundamental concept that forms the basis of building user interfaces (UIs). React encourages developers to break down the UI into small, reusable components, which are self-contained units responsible for their own rendering and behavior. These components can be composed together to form complex UI structures.

Here are the key aspects of Component-Based Architecture in React:

  1. Component Composition: React allows you to build UIs by composing components together. Each component represents a specific part of the UI, and they can be combined to create more complex components or entire application screens. Components can be nested within each other to form a tree-like structure, with each component responsible for its own rendering and behavior.
  2. Reusability: Components in React are designed to be reusable. Once a component is created, it can be used multiple times throughout the application, promoting code reuse and reducing duplication. Reusable components help to maintain consistency in UI design and make development more efficient.
  3. Component State: React components can have their own internal state, which represents data that can change over time. State allows components to manage their own data and handle user interactions. By managing state within components, React enables developers to create interactive and dynamic UIs.
  4. Component Lifecycle: React components have a lifecycle that consists of different phases, such as mounting, updating, and unmounting. This lifecycle provides hooks or methods that can be used to perform actions at specific points in a component's life. For example, you can initialize state, fetch data, or perform cleanup operations when a component is mounted or unmounted.
  5. Props: Props (short for properties) are a way to pass data from a parent component to its child components. Props allow components to receive inputs and customize their behavior or appearance based on those inputs. Props are read-only and cannot be modified by the child components, making the data flow in React one-way.
  6. Component-Based Styling: Component-Based Architecture in React extends beyond just structure and behavior; it also applies to styling. React provides various approaches for styling components, including CSS-in-JS libraries, CSS modules, and CSS preprocessors. Styling is often scoped to individual components, ensuring encapsulation and reusability.

 

import React from 'react';

// Example component: Header
const Header = () => {
  return (
    <header>
      <h1>Welcome to My App!</h1>
    </header>
  );
};

// Example component: Article
const Article = () => {
  return (
    <article>
      <h2>Article Title</h2>
      <p>This is the content of the article.</p>
    </article>
  );
};

// Example component: Sidebar
const Sidebar = () => {
  return (
    <aside>
      <h3>Sidebar</h3>
      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
      </ul>
    </aside>
  );
};

// Example component: App
const App = () => {
  return (
    <div>
      <Header />
      <div className="container">
        <Article />
        <Sidebar />
      </div>
    </div>
  );
};

export default App;

In this example, we have four components: Header, Article, Sidebar, and App. Each component represents a specific part of the application's user interface.

  • The Header component displays a welcome message.
  • The Article component displays the title and content of an article.
  • The Sidebar component shows a list of items.
  • The App component is the root component of our application. It renders the Header, Article, and Sidebar components in the desired structure.

By breaking down the UI into reusable components, we can build complex user interfaces by composing these smaller components together. Each component is responsible for its specific functionality and presentation, making the code more modular and maintainable.

728x90
Comments