GithubHelp home page GithubHelp logo

react-hooks-fragments's Introduction

BONUS: React Fragments

Learning Goals

  • Use fragments in React components to return top-level elements

Why Fragments

It is required that every React component must return a single JSX element. Because of this, we often use elements such as div to wrap other elements within our JSX. When rendered, this creates a DOM element for that outer div, which is sometimes unnecessary. For example:

function ChildComponent() {
  return (
    <div>
      <p>Hey, I am a child</p>
      <p>My name is child component</p>
    </div>
  );
}

function ParentComponent() {
  return (
    <div className="parent">
      <ChildComponent />
      <ChildComponent />
    </div>
  );
}

This setup creates a DOM structure that looks like this:

<div class="parent">
  <div>
    <p>Hey, I am a child</p>
    <p>My name is child component</p>
  </div>
  <div>
    <p>Hey, I am a child</p>
    <p>My name is child component</p>
  </div>
</div>

Those nested divs don't have any purpose here and don't have any styling besides their default properties. Without them though, we would have an error as there are two p tags being returned in the ChildComponent. Instead, we could use React Fragments, preventing the extra divs from being added to the DOM:

function ChildComponent() {
  //The wrapping 'div' here has been replaced with a React fragment
  return (
    <React.Fragment>
      <p>Hey, I am a child</p>
      <p>My name is child component</p>
    </React.Fragment>
  );
}

function ParentComponent() {
  return (
    <div>
      <ChildComponent />
      <ChildComponent />
    </div>
  );
}

With the fragment in place, the DOM will now look like this:

<div>
  <p>Hey, I am a child</p>
  <p>My name is child component</p>
  <p>Hey, I am a child</p>
  <p>My name is child component</p>
</div>

<React.Fragment> allows a component to return multiple elements without adding a wrapper element that adds to the DOM.

You can also use the shorthand syntax for fragments to make your JSX cleaner:

function ChildComponent() {
  // <> === <React.Fragment>
  return (
    <>
      <p>Hey, I am a child</p>
      <p>My name is child component</p>
    </>
  );
}

Fragments are not restricted to the outermost element being returned in JSX. Imagine you had an array of book objects in your props that you want rendered to the DOM. Each book has multiple attributes you want to display, but you don't need an element that wraps around these attributes. A fragment can be used here, and can still take a key attribute:

function Bookshelf(props) {
  return (
    <section>
      {props.books.map((book) => (
        <React.Fragment key={book.id}>
          <h1>{book.title}</h1>
          <h2>{book.author}</h2>
        </React.Fragment>
      ))}
    </section>
  );
}

Conclusion

Fragments are a small addition to React overall, but when used properly, can reduce a lot of unnecessary DOM bloat. They allow us a bit more flexibility in how we write our components, eliminating the need for wrapper elements.

Resources

react-hooks-fragments's People

Contributors

ihollander avatar lizbur10 avatar thuyanduong-flatiron avatar jlboba avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.