DevOps | Cloud | Analytics | Open Source | Programming





How To Fix - "Parse Error : Adjacent JSX Elements Must Be Wrapped in An Enclosing Tag" ?



In this post, we will see - How To Fix - "Parse Error : Adjacent JSX Elements Must Be Wrapped in An Enclosing Tag".


Uncaught Error: Parse Error: Line xx: Adjacent JSX elements must be wrapped in an enclosing tag


Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. 
Did you want a JSX fragment <>...</>?


Syntax error: Adjacent JSX elements must be wrapped in an enclosing tag

  React has to return only one element. If you have to return more than one elements, then put all the tags(child) within another element tag(parent). And then return that parent. There are two things to provide to a component tree - a node (element) or a collection of nodes in React. There are certain ways that you have to take up to handle this error.   You can use one or the other from the below solutions based on what version of React you are using.  

Option 1:

 

  • Put the "component" between an enclosing tag if you have missed. If you have missed that, there are chances - somewhere in the code two elements are returned simultaneously. This option can be useful if you are using a very old version of React.
 


WRONG CODE 

return ( 
   <Component />
   <Component />
)


RIGHT CODE

return (
   <div id="parent">
     <Component />
     <Component />
   </div>
)

 

Option 2:

  • Put the "component" within an array and then return that array. But note that this thing might not work prior to React 16. But you have to keep track of certain things for the array -
    • The children must be separated by commas.
    • The children requires to have a key to prevent "key warning".
    • The strings has to be wrapped in quotes.

WRONG CODE 
return ( 
    <Component />
    <Component />
)


RIGHT CODE
return ( 
   \[
     <Component />,
     <Component />
   \]
)

OR


RIGHT CODE
return ( 
   \[
     <Component key="0" />,
     <Component key="1" />
   \]
)

 

Option 3:

  • Use Fragments which can return the children directly. But it is available only from React 16 onwards. Fragments are components that doesn't have nodes and can act as a good alternative of using arrays. These are -
    • A bit faster,
    • Uses less memory,
    • Doesn't require to create extra DOM node - so less cluttered DOM tree.

return (
  <React.Fragment>
    <Component />,
    <Component />
  </React.Fragment>
);

 

Option 4:

  • Use the JSX syntax <></> . But be aware that this requires babel v7 support.

return (
  <>
    <Component />,
    <Component />
  </>
);

 

Option 5:

  • We can use HOC -  Higher-Order Components. A higher-order component is a function that takes a component and returns a new component. HOC composes the original component by wrapping it in a container component. It is a pure function with zero side-effects. The wrapped component receives all the props of the container - along with a new prop - data. This is used to render the output. The HOC doesn't care about the use of the data.
  Hope this helps to fix the error.    

Other Interesting Reads -

     


 Uncaught Error: Parse Error: Line xx: Adjacent JSX elements must be wrapped in an enclosing tag ,Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. ,Did you want a JSX fragment <>...</>? ,Syntax error: Adjacent JSX elements must be wrapped in an enclosing tag ,vue parsing error: adjacent jsx elements must be wrapped in an enclosing tag ,unterminated jsx contents ,parsing error: adjacent jsx elements must be wrapped in an enclosing tag eslint ,react fragment ,expected corresponding jsx closing tag for img ,jsx expressions must have one parent element ,enclosing tag html ,expected corresponding jsx closing tag for <li , , ,parsing error: adjacent jsx elements must be wrapped in an enclosing tag eslint ,jsx expressions must have one parent element means ,unterminated jsx contents ,expected corresponding jsx closing tag for img ,react fragment ,jsx expressions must have one parent element jquery ,react-tag , ,adjacent jsx elements must be wrapped in an enclosing tag ,adjacent jsx elements must be wrapped in an enclosing tag react native ,vue parsing error adjacent jsx elements must be wrapped in an enclosing tag ,eslint parsing error adjacent jsx elements must be wrapped in an enclosing tag ,eslint adjacent jsx elements must be wrapped in an enclosing tag ,adjacent jsx elements must be wrapped in an enclosing tag vue ,adjacent jsx elements must be wrapped in an enclosing tag tr , , ,error parsing error adjacent jsx elements must be wrapped in an enclosing tag ,eslint parsing error adjacent jsx elements must be wrapped in an enclosing tag ,parsing error adjacent jsx elements must be wrapped in an enclosing tag vue ,parsing error adjacent jsx elements must be wrapped in an enclosing tag. did you want a jsx fragment ,vue parsing error adjacent jsx elements must be wrapped in an enclosing tag , , ,Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. ,parsing error adjacent jsx elements must be wrapped in an enclosing tag. did you want a jsx fragment ,parsing error adjacent jsx elements must be wrapped in an enclosing tag vue ,eslint parsing error adjacent jsx elements must be wrapped in an enclosing tag ,error parsing error adjacent jsx elements must be wrapped in an enclosing tag ,Adjacent JSX elements must be wrapped in an enclosing tag ,adjacent jsx elements must be wrapped in an enclosing tag ,adjacent jsx elements must be wrapped in an enclosing tag heuer ,adjacent jsx elements must be wrapped in an enclosing tag jsx ,adjacent jsx elements must be wrapped in an enclosing tag questions ,adjacent jsx elements must be wrapped in an enclosing tag react ,adjacent jsx elements must be wrapped in an enclosing tag react native ,adjacent jsx elements must be wrapped in an enclosing tag tr ,adjacent jsx elements must be wrapped in an enclosing tag vue ,adjacent jsx elements must be wrapped in an enclosing tag. did you want a jsx ,error parsing error adjacent jsx elements must be wrapped in an enclosing tag ,eslint adjacent jsx elements must be wrapped in an enclosing tag ,eslint parsing error adjacent jsx elements must be wrapped in an enclosing tag ,inline babel script adjacent jsx elements must be wrapped in an enclosing tag ,parsing error adjacent jsx elements must be wrapped in an enclosing tag ,parsing error adjacent jsx elements must be wrapped in an enclosing tag eslint ,parsing error adjacent jsx elements must be wrapped in an enclosing tag vue ,react adjacent jsx elements must be wrapped in an enclosing tag ,react native adjacent jsx elements must be wrapped in an enclosing tag ,syntax error adjacent jsx elements must be wrapped in an enclosing tag ,vue adjacent jsx elements must be wrapped in an enclosing tag ,vue parsing error adjacent jsx elements must be wrapped in an enclosing tag