I'm trying to render a button in React using perfectly valid JSX (at least, as far as I can tell after looking at it for 20 minutes). But for some reason I keep getting the syntax error:
Unexpected token '<' on line 18
This comes as a surprise to me considering when I load the script I'm both using the text/babel
type attribute, and the babel script is loaded in the <head>
while this one is loaded in the DOM.
Here's what my HTML looks like:
<head>
<!-- react, react-dom, axios etc. go here !-->
<script crossorigin src="https://unpkg.com/babel-standalone@6.26.0/babel.min.js"></script>
</head>
<body>
<div id="content"></div>
<!-- notice that i'm using babel !-->
<script type="text/babel" src="cdn/static/scripts/module/user.js">
</body>
And here is my JavaScript:
class User extends Component{
constructor(props){
super();
this.user = props.user;
this.state = {
modalOpen: false
};
}
Button = () => {
let user = this.user;
// vvvv this is what the error points to
return <button className={(user.loggedIn ? "user" : "sign-in")}>{(user.loggedIn ? user.public.display : "Sign In")}</button>;
}
render(){
return (
<div className="button">
<this.Button />
</div>
);
}
}
ReactDOM.render(<User user={window.User} />, document.getElementById("content"));
What is even stranger is that it will actually render the button
, but because there is an error, it will also cause the rest of my JavaScript to break.
I've never come across this problem before so all help is appreciated, cheers.