🐛 React state init Unexpected token in Component
See original GitHub issue🎛 Configuration (.babelrc, package.json, cli command)
Here’s my basic setup package.json
{
"scripts": {
"start": "./node_modules/parcel-bundler/bin/cli.js ./public/index.html"
},
"devDependencies": {
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"parcel-bundler": "^1.6.1"
},
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0"
}
}
.babelrc
{
"presets": ["env", "react"]
}
Couldn’t get any simpler!
🤔 Expected Behavior
This bit of code works totally fine with a create-react-app
import React, { Component } from 'react';
class Counter extends Component {
state = {
counter: 0
};
render() {
const {count} = this.state;
return (
<div>
<button
onClick={() => this.setState({count: count + 1})}
>
Increment counter
</button>
<p>Count: {count}</p>
</div>
);
}
}
export default Counter;
😯 Current Behavior
However, when used with the previously mentioned config at the top I get this error:
🚨 /Users/Alexandre/WebstormProjects/ReactJS/parceljs-react/src/Counter.js:4:10: Unexpected token (4:10)
2 |
3 | class Counter extends Component {
> 4 | state = {
| ^
5 | counter: 0
6 | };
7 |
💁 Possible Solution
In order to make it work I had to init the state inside a constructor, like so:
class Counter extends Component {
constructor() {
super();
this.state = { count: 0 }
}
...
}
Any Idea as to what could cause this?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:16
- Comments:9 (3 by maintainers)
Top Results From Across the Web
React - "Unexpected token" when initialising state
I hadn't read the error properly so I focused the input part. But the actual problem was different. Actually, answers should explain why...
Read more >Unexpected Token Error in React Router Component
The JavaScript exceptions "unexpected token" occurs when a specific language construct was expected, but something else was provided. This might ...
Read more >SyntaxError: Unexpected token" but no token is specified, the ...
Coding example for the question "SyntaxError: Unexpected token" but no token is specified, the error is indicated to be on whitespace-React Native.
Read more >SyntaxError: Unexpected token '<' on new build of React
I have a React application and every time I deploy it to digital ocean I'm getting this error SyntaxError: Unexpected token '<', but...
Read more >Uncaught SyntaxError: Unexpected token '<' in video sdk - Web
It keep getting error when joining/creating the session. There are two errors below. Do I miss anything ? Error Uncaught SyntaxError: Unexpected ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

You need
babel-plugin-transform-class-propertiesas well. (https://babeljs.io/docs/plugins/transform-class-properties/)This needs to be added to the babelrc file:
@polmoneys sure, package.json
.babelrc
Having issues ?