Adjacent JSX elements must be wrapped in an enclosing tag

The "Adjacent JSX elements must be wrapped in an enclosing tag" error happens in React, whenever you try to return more than one element from your React component. To solve this error,  we should wrap the multiple elements in a parent div element or wrap elements using a react fragment.

Below example code throws the error.

// This will throw the above error
export default function App() {
  return (
    <P>First Element</P>
    <P>Second Element</P>
  );
}

In the above example, the issue is coming from the return statement of the component. Here we return two <p> tags from the App component. But react can only return one element from a component.

To resolve this error, we need to wrap all root sibling elements into a single element. For this, we have two methods.

  • Wrap all elements in React Fragment
  • Wrap all elements in a parent div

Let us discuss all these in detail.

Wrap all elements in React Fragment

We can wrap all elements in react fragment as given below example.

import React from 'react';

export default function App() {
  return (
  	<React.Fragment>
    	<P>First Element</P>
    	<P>Second Element</P>
    </React.Fragment>
  );
}

Fragments are used when we need to group a list of elements without adding extra nodes to the DOM. There is a new, shorter syntax we can use for declaring fragments. It looks like an empty tag as given below.

import React from 'react';

export default function App() {
  return (
  	<>
    	<P>First Element</P>
    	<P>Second Element</P>
    </>
  );
}

 

Wrap all elements in a parent div

We can wrap all the root sibling elements in a parent element like div.

export default function App() {
  return (
  	<div>
    	<P>First Element</P>
    	<P>Second Element</P>
    </div>
  );
}

The disadvantage of using a div element as a wrapper is that it will end up with an extra DOM element for no reason. This can cause the layout to break when working with the CSS Flexbox and Grid. This may also lead to invalid HTML for elements that must follow a specific structure like ul > li and table>tr>td. That is why we prefer to use the built-in React.Fragment element instead. React fragments serve as a cleaner alternative to using unnecessary divs in our code.

Why adjacent elements to be wrapped into a single element?

Under the hood, React components are just functions that return React.createElement function calls from our components. So when we return multiple elements at the same level, it is similar to multiple return statements at the same level of a function as given below.

export default function App() {
    return React.createElement('p', null, 'First Element');
    return React.createElement('p', null, 'Second Element');
}

In the above code, the second return statement is unreachable and it is an invalid syntax. This is the reason we can only return one root element from your React components.

Conclusion

To solve the "Adjacent JSX elements must be wrapped in an enclosing tag" error, wrap all return elements of a component using a React Fragment or in a parent div.