DEV Community

Cover image for πŸš€ Day 11: React Fragments and Composition vs Inheritance in React πŸš€
Mayuresh Surve
Mayuresh Surve

Posted on

πŸš€ Day 11: React Fragments and Composition vs Inheritance in React πŸš€

Today, I explored two fundamental and interrelated concepts in React: Composition vs Inheritance and React Fragments. These concepts are crucial for building efficient, clean, and maintainable React applications. Here’s what I learned:

Component Composition vs Inheritance

In traditional object-oriented programming, inheritance is often used to extend and reuse code. However, in React, composition is preferred over inheritance. This means building complex UIs by combining smaller, reusable components rather than relying on hierarchical inheritance structures.

Component Composition

Composition in React is about assembling components to create more complex interfaces. By composing components, you can build UIs that are flexible, maintainable, and easy to extend. For instance, a Card component can be composed with other components like Header, Body, and Footer to create a full-featured card UI:

function Card({ children }) {
  return <div className="card">{children}</div>;
}

function App() {
  return (
    <Card>
      <h2>Title</h2>
      <p>This is some text inside the card.</p>
    </Card>
  );
}
Enter fullscreen mode Exit fullscreen mode

Children and Props

React provides a powerful way to pass data and elements between components using props and the special children prop. Props allow components to be configured and reused with different data, while children lets you nest elements inside a component:

function Wrapper({ children }) {
  return <div className="wrapper">{children}</div>;
}

function App() {
  return (
    <Wrapper>
      <h1>Hello, World!</h1>
      <p>This is inside the wrapper component.</p>
    </Wrapper>
  );
}
Enter fullscreen mode Exit fullscreen mode

Containment and Specialization

Containment is a pattern where a component (like a modal or a layout) contains other components. Specialization is when a generic component is extended or customized to create a more specific version, such as creating PrimaryButton and SecondaryButton components from a base Button component.

function Button({ children, className, ...props }) {
  return (
    <button className={`button ${className}`} {...props}>
      {children}
    </button>
  );
}

function PrimaryButton(props) {
  return <Button className="primary-button" {...props} />;
}

function SecondaryButton(props) {
  return <Button className="secondary-button" {...props} />;
}

function App() {
  return (
    <div>
      <PrimaryButton onClick={() => alert("Primary Button Clicked!")}>
        Primary Button
      </PrimaryButton>
      <SecondaryButton onClick={() => alert("Secondary Button Clicked!")}>
        Secondary Button
      </SecondaryButton>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Avoiding Inheritance in React

React encourages avoiding inheritance because it can lead to more complex and less flexible code. Instead, composition allows you to reuse code without the constraints of a strict inheritance hierarchy, making your components more modular and easier to manage.

React Fragments

Next, I delved into React Fragments, which are used to group multiple elements without adding extra nodes to the DOM. This helps keep the DOM structure clean and avoids unnecessary wrappers.

Using React Fragments

Fragments allow you to return multiple elements from a component without creating an additional parent element:

function FragmentExample() {
  return (
    <React.Fragment>
      <h1>Title</h1>
      <p>This is a paragraph.</p>
    </React.Fragment>
  );
}
Enter fullscreen mode Exit fullscreen mode

Avoiding Unnecessary Wrapping Elements

Using unnecessary wrapper elements can clutter the DOM. Fragments help prevent this by grouping elements without adding extra nodes:

function NoFragmentExample() {
  return (
    <div>
      <h1>Title</h1>
      <p>This is a paragraph.</p>
    </div>
  );
}

function FragmentExample() {
  return (
    <>
      <h1>Title</h1>
      <p>This is a paragraph.</p>
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

Short Syntax for Fragments

React provides a shorthand for fragments using empty tags (<> and </>), making the code cleaner and more concise:

function ShortSyntaxExample() {
  return (
    <>
      <h1>Title</h1>
      <p>This is a paragraph.</p>
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

When to Use Fragments

Use fragments whenever you need to group multiple sibling elements without adding extra DOM nodes. They are particularly useful when returning lists or mapping over an array to generate elements.

Key Takeaways

  • Composition is more flexible and maintainable than inheritance in React.
  • Fragments help keep the DOM clean by avoiding unnecessary wrapper elements.

Conclusion

Today's learning journey deepened my understanding of how React encourages building applications using composition and how to maintain a clean DOM with fragments. These skills are essential for creating scalable and maintainable applications, and I’m excited to continue applying them in my projects.

Stay tuned for more updates as I continue my React learning journey!

Stay updated with my progress by following my LinkedIn and GitHub for detailed posts and code samples.

Top comments (0)