Unexpected token '=' in React Component [duplicate]

寵の児 提交于 2019-11-26 16:44:10

You need to use transform-class-properties plugin to use class fields, You can install it like

npm install --save-dev babel-plugin-transform-class-properties

and use it as a plugin

{
    "presets": ["env", "react"],
    "plugins": ["transform-object-rest-spread", "transform-class-properties"]
}   

transform-object-rest-spread is used for the rest spread syntax which is like

const {a, b, ...rest} = this.props

According to the documentation:

This presents two related proposals: "class instance fields" and "class static fields".

"Class instance fields" describe properties intended to exist on instances of a class (and may optionally include initializer expressions for said properties).

"Class static fields" are declarative properties that exist on the class object itself (and may optionally include initializer expressions for said properties).

This proposal is currently at Stage 2.

You can also solve this by using preset stage-2 by installing

npm install --save-dev babel-preset-stage-2

and using it like

{
    "presets": ["env", "react", "stage-2"],
    "plugins": ["transform-object-rest-spread"]
} 

you cant add stage-0 preset

{
    "presets": ["env", "react", "stage-0"],
    "plugins": ["transform-object-rest-spread"]
}

and dont forget run

yarn add babel-preset-stage-0
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!