Categories
React Answers

How to avoid extra wrapping div in React?

Spread the love

Sometimes, we want to avoid extra wrapping div in React.

In this article, we’ll look at how to avoid extra wrapping div in React.

How to avoid extra wrapping div in React?

To avoid extra wrapping div in React, we can use a fragment.

For instance, we write

const Comp = () => {
  //...
  return (
    <React.Fragment>
      <ChildA />
      <ChildB />
      <ChildC />
    </React.Fragment>
  );
};

to wrap the components ChildA, ChildB and ChildC with React.Fragment to render ChildA, ChildB and ChildC together without wrapping them with an extra container element.

Conclusion

To avoid extra wrapping div in React, we can use a 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 *