JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

JSX expressions must have one parent element error in React

Author:JIYIK Last Updated:2025/03/17 Views:

React.js error "JSX expression must have a parent element" occurs when a component returns multiple elements. To resolve the error, wrap the elements in a parent divelement or use a fragment, for example <><h2>One</h2><h2>Two</h2></>.

Here is an example that produces the above error

export default function App() {
  // ⛔️ JSX expressions must have one parent element.
  return (
    <h2>One</h2>
    <h2>Two</h2>
  );
}

JSX expressions must have one parent element error in React

The problem in the example above is that the App component returns multiple elements.

A component cannot return multiple elements, just like a function cannot return multiple values ​​(unless they are wrapped in an array, which is a single value).

One way to fix the error is to use react fragments.

export default function App() {
  return (
    <>
      <h2>One</h2>
      <h2>Two</h2>
    </>
  );
}

Fragments are used when we need to group sublists without adding extra nodes to the DOM.

We may also see a more verbose snippet syntax used.

import React from 'react';

export default function App() {
  return (
    <React.Fragment>
      <h2>One</h2>
      <h2>Two</h2>
    </React.Fragment>
  );
}

The two examples above achieve the same result—they group a list of child elements without adding extra nodes to the DOM.

Most code editors now support a more concise syntax.

Another solution is to wrap our child element in another DOM element, such as a div.

export default function App() {
  return (
    <div>
      <h2>One</h2>
      <h2>Two</h2>
    </div>
  );
}

This resolves the error because divinstead of returning multiple elements we are now returning a single element with multiple children.

This approach only works if adding additional divwill not break our layout, otherwise use fragments as fragments do not add any extra markup to the DOM.

We might also see errors occur in conditional statements if one of the code paths returns multiple elements at the same level.

export default function App() {
  return (
    <div>
      {/* ⛔️ JSX expressions must have one parent element.ts(2657) */}
      {true ? (
        <h2>One</h2>
        <h2>Two</h2>
      ) : null}
    </div>
  );
}

The truthy path of the ternary operator returns 2 sibling elements, causing the error. We can fix this by wrapping the two elements with a fragment.

export default function App() {
  return (
    <div>
      {true ? (
        <>
          <h2>One</h2>
          <h2>Two</h2>
        </>
      ) : null}
    </div>
  );
}

Now every code path of the ternary returns a value.

The reason for the "JSX expression must have a parent element" error is that the syntax for returning multiple values ​​from a function is invalid.

React components are just functions, so when we return multiple elements at the same level, we are actually using multiple returnstatements at the same level in a function.

function render() {
  return React.createElement('h2', null, 'One');
  return React.createElement('h2', null, 'Two');
}

The second returnstatement is not accessible; it is invalid syntax.

On the other hand, when we wrap the element with a fragment or another element, the function returns just one value, which solves the error.

Another approach we might look at is to group the elements into an array.

export default function App() {
  return [<h2 key={0}>One</h2>, <h2 key={1}>Two</h2>];
}

The array is a single value, so the error is resolved, but we need to pass a unique key prop to each array element.

This is unnecessary and should be avoided, as the snippet syntax is more readable and intuitive.

export default function App() {
  return (
    <>
      <h2>One</h2>
      <h2>Two</h2>
    </>
  );
}

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to avoid cross-origin (CORS) issues in React/Next.js

Publish Date:2025/03/17 Views:166 Category:NETWORK

In this article, we will introduce how to avoid cross-origin (CORS) issues in React/Next.js. Cross-origin resource sharing (CORS) is a protocol that defines how web requests should be handled when crossing different URLs.

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:185 Category:React

React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.

React Tutorial: Props Anti-Pattern

Publish Date:2025/03/16 Views:183 Category:React

React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.

React Tutorial - Props Validation

Publish Date:2025/03/16 Views:99 Category:React

Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:58 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:187 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial