DEV Community

Cover image for The React Way - 1
Pierre Olivier TRAN
Pierre Olivier TRAN

Posted on

The React Way - 1

Just as a well-organized codebase is key to the maintainability of a software project, understanding and implementing Composition in React is crucial for creating efficient and manageable user interfaces. Drawing inspiration from the principles of "Clean Code," let's explore how Composition in React can be akin to the art of code splitting and maintaining a clean, orderly codebase.

What is Composition in React?

Think of your React application as a large codebase. In "Clean Code," one principle is to keep things modular - small, focused modules are easier to maintain than large, monolithic ones. Composition in React works similarly. It allows you to create a complex UI by combining smaller, reusable components, much like breaking a large codebase into manageable modules.

BUT WHY ?

  • Coding is good
  • Coding small is better
  • Coding small and clean is best

And just as any feature can be split into smaller, more manageable features, so does your React codebase.

Example of Basic Composition

Consider a scenario where you're building a user interface with a Header, Footer, and Content. Each part of the interface is like a module in a well-organized codebase:

function Page() {
  return (
    <div>
      <Header />
      <Content />
      <Footer />
    </div>
  );
}

function Header() {/* ... */}
function Content() {/* ... */}
function Footer() {/* ... */}

Enter fullscreen mode Exit fullscreen mode

Each component (Header, Content, Footer) is self-contained, similar to how each module in a codebase should have a single responsibility.

Props and Composition

In "Clean Code," clear and concise function signatures are vital. Similarly, in React, props should be used to make components flexible yet straightforward. Props are like function parameters, allowing you to pass data and behavior down to child components, ensuring that each component remains modular and single-purpose.

function Content({ articles }) {
  return (
    <div>
      {articles.map(article => <Article key={article.id} {...article} />)}
    </div>
  );
}

function Article({ title, content }) {
  return (
    <div>
      <h2>{title}</h2>
      <p>{content}</p>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Higher Order Components and Composition

Higher Order Components (HOCs) in React are analogous to decorators in software design, as described in "Clean Code." They add additional functionalities to components without modifying their core responsibilities.

function withLogging(WrappedComponent) {
  return function LoggingComponent(props) {
    console.log('Component rendered with props:', props);
    return <WrappedComponent {...props} />;
  };
}

const LoggedContent = withLogging(Content);
Enter fullscreen mode Exit fullscreen mode

This HOC adds logging functionality to any component, enhancing its capabilities while keeping the original component's purpose intact.

TL;DR

Composition in React allows for building complex UIs with a similar mindset. By breaking down UIs into smaller, reusable components, managing props effectively, and using patterns like HOCs, you can create clean, efficient, and maintainable React applications. Embracing these practices will not only make your code more understandable but also enhance its overall scalability and robustness.

Top comments (0)