我遇到了一个我根本无法解释的语法错误。
代码:
import React, { Component } from 'react';
class Button extends Component{
handleClick = () => {
this.props.onClickFunction(this.props.incrementValue)
}
render() {
return (
<button onClick={this.handleClick}>
+{this.props.incrementValue}
</button>
);
}
}
错误消息 - 意外令牌 (4:14):
2 |
3 | class Button extends Component{
> 4 | handleClick = () => {
| ^
5 | this.props.onClickFunction(this.props.incrementValue)
6 | }
7 |
我以前使用过这段代码,但我想尝试使用 webpack,并且由于这些更改,我收到了此错误。据我了解,这是 es2015 中引入的新语法。我相信我已经正确配置了一切:
"devDependencies": {
"axios": "^0.17.1",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"bootstrap": "^4.0.0-beta.2",
"font-awesome": "^4.7.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-fontawesome": "^1.6.1",
"react-scripts": "1.0.17",
"reactstrap": "^5.0.0-alpha.4",
"webpack": "~3.9.1",
"webpack-dev-server": "^2.9.5"
}
module.exports = {
entry: "./index.js",
output:{
filename:"public/bundle.js"
},
module:{
loaders:[
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query:{
presets:['react', 'es2015']
}
}
]
}
}
我的第一个想法是,也许我对 es2015 的配置不正确。但是我尝试使用正常的函数语法仍然收到以下错误:
2 |
3 | class Button extends Component{
> 4 | handleClick = function(){
| ^
5 | this.props.onClickFunction(this.props.incrementValue)
6 | }
7 |