phantom_wizard
phantom_wizard

Reputation: 3168

Simple react component not accepting arrow function

I'm trying to create my first component in react but I keep getting error. It results in not showing button element on the website at all. Here are my files:

ERROR in ./src/js/components/presentational/Button1.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: Unexpected token (3:13)

  1 | import React, { Component } from "react";
  2 | class Button1 extends React.Component {
> 3 |   handleClick = () => {
    |               ^
  4 |       console.log("dupa");
  5 |   };
  6 |   render() {

./src/js/components/presentational/Button1.js

import React, { Component } from "react";
class Button1 extends React.Component {
    handleClick = () => {
        console.log("dupa");
    };
    render() {
        return (
        <button onclick={this.props.handleClick}>
            Button
        </button>
        );
    }
}
export default Button1;

./src/js/components/container/FormContainer.js

import React, { Component } from "react";
import ReactDOM from "react-dom";
import Button1 from "../presentational/Button1";
class FormContainer extends Component {
  render() {
    return (
        <Button1 />
    );
  }
}
export default FormContainer;
const wrapper = document.getElementById("create-article-form");
wrapper ? ReactDOM.render(<FormContainer />, wrapper) : false;

./src/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" >
    <title>How to set up React, Webpack, and Babel</title>
</head>
<body>
    <div class="container">
        <div class="row mt-5">
            <div class="col-md-4 offset-md-1">
                <p>Create a new article</p>
                <div id="create-article-form">
                    <!-- form -->
                </div>
            </div>
        </div>
    </div>
</body>
</html>

EDIT

I thought I was using babel. Do I need some additional step for transpilation? Currently I'm using only

npm start

This is my package.json:

{
  "name": "front-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --open --mode development",
    "build": "webpack --mode production",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "babel-preset-latest": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "prop-types": "^15.6.2",
    "react": "^16.4.2",
    "react-dom": "^16.4.2",
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.5"
  }
}

and this is my .babelrc

{
  "presets": [
    "react",
    "env"
  ]
}

EDIT2: Problem solved. As stated here it was because arrow functions are not included into standard right now. I had to run sudo npm install --save-dev babel-plugin-transform-class-properties and had to edit my .babelrc:

{
  "presets": [
    "react",
    "env"
  ],
  "plugins": [
    ["transform-class-properties", { "spec": true }]
  ]
}

Upvotes: 3

Views: 286

Answers (2)

ashfaq.p
ashfaq.p

Reputation: 5469

You can also do:

handleClick() {
   // code here
}

Upvotes: 1

Tholle
Tholle

Reputation: 112787

You get the error because class properties (class Example { myProperty = 'foobar' }) is not a part of the language yet.

You need to add either a Babel plugin or preset for it to work. You could use the stage 2 preset.

.babelrc

{
  "presets": [
    "react",
    "env"
    "stage-2"
  ]
}

Upvotes: 6

Related Questions