I have below code in react js.
class Posts extends Component {
render() {
return (
{console.log('test')}
);
}
}
After running this code I get error which is
Parsing error: Unexpected token, expected ","
8 | return (
> 9 | {console.log('test')}
| ^
10 | );
11 | }
12 | }
UPDATE
wrapping it up with parent tag returns the same error
> 9 | <div>
| ^
10 | {console.log('nothing')}
11 | </div>
UPDATE
here is the whole class
import React, { Component } from 'react';
import { connect } from 'react-redux';
class Post extends Component {
render() {
return (
<div>
{console.log('test')}
</div>
);
}
}
const mapStateToProps = (state) => {
return {
posts: state
}
};
export default Post;
So, to fix the unexpected token problem, go through your codes for punctuation errors. Punctuation marks like parenthesis and commas must be proper in your coding. If not, JavaScript won't recognize it and won't be able to parse it.
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.
Like other programming languages, JavaScript has define some proper programming rules. Not follow them throws an error.An unexpected token occurs if JavaScript code has a missing or extra character { like, ) + – var if-else var etc}. Unexpected token is similar to syntax error but more specific.
You always need to have a wrapping parent JSX tag when returning jsx. Since you don't have any jsx tags it's just invalid javascript. Should be:
class Posts extends Component {
render() {
return console.log('test')
}
}
or if you want the jsx
class Posts extends Component {
render() {
return (
<div>
{console.log('test')}
</div>
)
}
}
edit (2020-1-19)
jsx now supports empty tags for parent closures:
class Posts extends Component {
render() {
return (
<>
{console.log('test')}
</>
)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With