0

I am very much new to React. The following is my first script.

But I am getting the following error.

Failed to compile

./src/components/Layout/index.js
Syntax error: Unexpected token (12:5)

  10 | const Layout = (props) => {
  11 |   return(
> 12 |     <>
     |      ^
  13 | 
  14 |    <Header />
  15 |    {props.children}

here is my Layout component code :

import React from 'react';
import Header from '../Header';


/**
* @author
* @function Layout
**/

const Layout = (props) => {
  return(
    <>

   <Header />
   {props.children}
   </>
   )

 }

export default Layout;

Any idea how to fix it?

9
  • 6
    What version of Babel are you running? Commented Sep 27, 2021 at 13:19
  • Provide more information like your babel configuration and how you are compelling Commented Sep 27, 2021 at 13:20
  • 1
    And is your project setup to handle JSX in .js files? Or does it need to be .jsx Commented Sep 27, 2021 at 13:20
  • Have you tried adding a space after the word return? Or deleting the newline after the opening parenthesis? Commented Sep 27, 2021 at 13:20
  • 1
    @novonimo thank you so much , its works after adding <React.Fragment>...</React.Fragment> , but can you explain me why this error appears with <> </> ? Commented Sep 27, 2021 at 13:30

1 Answer 1

0

try using Fragment which came from the React library.

it provides an empty tag like <> </> but older versions of the JSX Babel plugin didn’t understand it.

as mentioned in the question's comments, it's an issue with your babel plugin version.

import React, {Fragment} from 'react';
import Header from '../Header';

const Layout = (props) => {
  return(
    <Fragment>
      <Header />
      {props.children}
   </Fragment>
   )

 }

export default Layout;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.