Delving Developer

Fixing the "JSX expressions must have one parent element" error

Eddie Cunningham
Eddie Cunningham
4 min readReact.js
Cover Image for Fixing the "JSX expressions must have one parent element" error

React is a popular JavaScript library built for creating interactive UI components. One of the common errors developers encounter while working with React components is the "JSX expressions must have one parent element" error. This tutorial will guide you through understanding this error and provide different solutions to fix it.

What causes the "JSX expressions must have one parent element" error?link

JSX is a syntax extension for JavaScript that allows developers to write HTML-like code within JavaScript. JSX is then transpiled to JavaScript before being executed by the browser. The "JSX expressions must have one parent element" error occurs when a JSX expression returns two or more sibling elements without encapsulating them within a single parent element.

Consider the following example:

function App() {
  return (
    <h1>Welcome to my app</h1>
    <p>Enjoy your stay</p>
  );
}

This will throw the "JSX expressions must have one parent element" error because the JSX expression has two sibling elements (<h1> and <p>) without a parent wrapper element.

To resolve the "JSX expressions must have one parent element" error, we suggest these four effective solutions:

  1. Utilizing React JSX Fragments
  2. Applying Custom HTML Containers
  3. Creating an Array of JSX Elements
  4. Implementing a Higher Order Component (HOC) in React

Solution 1: JSX Fragments

JSX fragments let you wrap a list of elements without adding additional nodes to the DOM. This is the most popular solution, and is commonly used with the short syntax of an empty tag <>...</>. Here's an example of fixing the above code using JSX fragments:

function App() {
  return (
    <>
      <h1>Welcome to my app</h1>
      <p>Enjoy your stay</p>
    </>
  );
}

Solution 2: Custom Containers

Another approach is to wrap your elements within a custom container such as a <div> or any other semantic HTML tag, depending on the context. Here's an example of fixing the error using a <div> container:

function App() {
  return (
    <div>
      <h1>Welcome to my app</h1>
      <p>Enjoy your stay</p>
    </div>
  );
}

Keep in mind that using custom containers will add extra nodes to the DOM, possibly impacting the structure and styling of your application.

Solution 3: Array of elements

In some situations, you might want to return an array of elements from a function instead of wrapping them within a container. Here's an example of fixing the error using an array of elements:

function App() {
  return (
    [
      <h1 key="heading">Welcome to my app</h1>,
      <p key="paragraph">Enjoy your stay</p>
    ]
  );
}

Note that each element in the array should have a unique key prop, as React uses this prop to optimize rendering.

Solution 4: Using a Higher Order Component (HOC)

A Higher Order Component (HOC) is a function that takes a component as an argument and returns a new component. It's a great way to reuse component logic. In this case, you can use an HOC to wrap your JSX elements within a container.

Here's an example of fixing the error using an HOC:

function withWrapper(WrappedComponent) {
  return function WrappedWithDiv(props) {
    return (
      <div>
        <WrappedComponent {...props} />
      </div>
    );
  };
}

function App() {
  return [
    <h1>Welcome to my app</h1>,
    <p>Enjoy your stay</p>,
  ];
}

export default withWrapper(App);

In this example, the withWrapper function takes the App component as an argument and returns a new component with a <div> wrapper.

Conclusionlink

As you can see, fixing the "JSX expressions must have one parent element" error can be done in various ways using JSX fragments, custom containers, arrays of elements, or HOCs. The best solution depends on your specific use case and the desired structure of your application.

For instance, if you want to have minimal impact on your DOM structure and avoid extra container nodes, you should opt for using JSX fragments. If your goal is to have a semantic structure or you need to provide a container for styling purposes, consider using custom containers like <div>, <section>, or <article>. If you require logic reuse and increase modularity within your application, using a Higher Order Component might be the perfect choice.

By understanding the cause of the "JSX expressions must have one parent element" error and learning various techniques to fix it, you can effectively solve the problem and improve your code structure and organization when working with React components.

Remember to choose the right solution based on your specific needs, and always keep in mind the importance of a clear and maintainable code base. With this knowledge in hand, you are now equipped to tackle this common error and create outstanding React applications.