简单的React组件不接受箭头功能

时间:2018-08-12 13:04:37

标签: javascript reactjs

我正在尝试在react中创建我的第一个组件,但是我一直遇到错误。这导致根本不在网站上显示按钮元素。这是我的文件:

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>

编辑

我以为我在用通天塔。我需要一些其他步骤进行移植吗?目前我只使用

npm start

这是我的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"
  }
}

这是我的.babelrc

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

EDIT2: 问题解决了。如此处所述,这是因为箭头功能目前未包含在标准中。我必须运行sudo npm install --save-dev babel-plugin-transform-class-properties,并且必须编辑.babelrc:

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

2 个答案:

答案 0 :(得分:6)

由于class propertiesclass Example { myProperty = 'foobar' })还不是该语言的一部分,您会收到此错误。

您需要添加Babel插件或预设才能正常工作。您可以使用stage 2预设。

.babelrc

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

答案 1 :(得分:1)

您也可以这样做:

handleClick() {
   // code here
}