Categories
JavaScript Answers

How to Avoid Extra Wrapping div in React?

Spread the love

To group multiple elements together without an extra wrapper div in React, we can use fragments.

To add them, we can use the React.Fragment component or <></> for short.

For instance, we can write:

import React from "react";

export default function App() {
  return (
    <>
      <div>a</div>
      <div>b</div>
      <div>c</div>
    </>
  );
}

or:

import React from "react";

export default function App() {
  return (
    <React.Fragment>
      <div>a</div>
      <div>b</div>
      <div>c</div>
    </React.Fragment>
  );
}

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *