question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

🐛 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:closed
  • Created 6 years ago
  • Reactions:16
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

46reactions
mischniccommented, Feb 19, 2018

You need babel-plugin-transform-class-properties as well. (https://babeljs.io/docs/plugins/transform-class-properties/)

This needs to be added to the babelrc file:

plugins": ["transform-class-properties"]
6reactions
speedtreammangacommented, Mar 16, 2018

@polmoneys sure, package.json

{
  "name": "parceljs-react",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "./node_modules/parcel-bundler/bin/cli.js ./public/index.html"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-plugin-transform-class-properties": "^6.24.1",
    "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"],
    "plugins": ["transform-class-properties"]
}

Having issues ?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found