Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

react : Unexpected token, expected ","

I have simple Component :

import React,{Fragment, useState} from "react";  
import './AppClass.css';

const AppClass =(props) => {
    const [isTrue, setIsTrue] = useState(false)
    
    return(
        <>
            <hr/>
            <h1 className="h1-red"> 
                {props.msg}
            </h1>
        </>
        {isTrue &&
            <>
            </hr>
            </>
        }
    )
}

export default AppClass;

But it keep yeling this error:

SyntaxError: C:\dev\my\go_apps\udemy_go_react\react-apps\my-app\src\AppClass.js: Unexpected token, expected "," (14:8)
  12 |             </h1>
  13 |         </>
> 14 |         {isTrue &&
     |         ^
  15 |             <>
  16 |             </hr>
  17 |             </>
ERROR in ./src/AppClass.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):

Im doing all as expected ..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your react tree should contain only one top level node. Try something like this:

import React,{Fragment, useState} from "react";  
import './AppClass.css';

const AppClass =(props) => {
    const [isTrue, setIsTrue] = useState(false);
    
    return(
        <>
            <hr/>
            <h1 className="h1-red"> 
                {props.msg}
            </h1>
            {isTrue &&
              <>
                <hr/>
              </>
            }
        </>
    )
}

export default AppClass;

Edit:
Plus you have used the tag </hr> which is treated as a closing tag in your example. Use <hr/> instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading