1

The render method in the React component looks like this:

render() {
        return <h1>Hello, {this.props.name}</h1>;
}

The error is this:

Module build failed (from ./node_modules/babel-loader/lib/index.js):
              SyntaxError: Unexpected token    20 |    21 |     render() { > 22
              |         return <h1>Hello, {this.props.name}</h1>;
              |      

Here is my package.json:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "babel-version": "babel --version"
  },
  "babel": {
    "presets": [
       "env",
       "react",
       "stage-2"
    ]
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ajv": "^6.0.0",
    "babel": "^6.23.0",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "css-loader": "^1.0.0",
    "less": "^3.8.0",
    "less-loader": "^4.1.0",
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "reactstrap": "^6.3.1",
    "style-loader": "^0.21.0",
    "webpack": "^4.16.3",
    "webpack-command": "^0.4.1"
  },
  "dependencies": {
    "bootstrap": "^4.1.3",
    "npm": "^6.3.0"
  }
}

Here is the whole class:

export default class SearchForm extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            name: ""
        };      
    }

    handleNameChange = (e) => {
        this.setState({ name: e.target.value });
    };

    render() {
        return <h1>Hello, {this.props.name}</h1>;
    }
}

Note the class doesn't make sense in terms of functionality at the moment because I've been experimenting to figure out the syntax error.

I have a .babelrc also:

{
  "plugins": [
    ["transform-class-properties"]
  ]
}

The method is pretty straightforward, so I can't see what the issue is. Any insight is appreciated.

17
  • I feel like there's an error with your class syntax not your JSX. Could you put your whole class? Commented Aug 3, 2018 at 23:59
  • Updated. Thanks. Commented Aug 4, 2018 at 0:08
  • 2
    Class properties (handleNameChange = (e) => { ... };) is a stage 2 proposal, so you most likely have to use "presets": [ "env", "react", "stage-2" ] Commented Aug 4, 2018 at 0:13
  • I think you shouldn't have the trailing semi colon after handleNameChange. Commented Aug 4, 2018 at 0:14
  • @EvanTrimboli That is perfectly valid, and often recommended. Commented Aug 4, 2018 at 0:15

1 Answer 1

1

You are most likely getting the error because the class property above (handleNameChange = (e) => { ... };) is a stage 2 proposal.

If you install babel-preset-stage-2 and add it to your .babelrc the error should be resolved.

.babelrc

{
  "presets": [
    ["env", "react", "stage-2"]
  ]
}
Sign up to request clarification or add additional context in comments.

5 Comments

Aren't these presets in .babelrc deprecated? Is there a more durable solution?
@Vidya I'm not sure I would use the word deprecated. The entire preset system is getting redone for Babel 7, but to my knowledge there is no alternative yet for Babel 6.
Ahh OK. Thanks.
@Vidya Stage presets are being deprecated in Babel 7. The alternative is to simply include the all plugins those presets come with - or more specifically, only the plugins you need. (So in this case, if all you need is babel-plugin-transform-class-properties you'd just include that.) See github.com/babel/babel/blob/…
That plugin was already included from the beginning. If I have babel-plugin-transform-class-properties and babel-preset-stage-2 and factor the .babelrc presets out into package.json, things fail again. I have to say that modern JS engineering is tougher than I expected. I can see why TypeScript, Reason, and Elm are so compelling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.