Wanted to use ES8 async/await for my project. Have been using it recently on ReactNative with Expo so didn't expect any problems on ReactJS. Although, the app can't build now... This is the error I get:
Syntax error: C:/projects/project1/src/containers/OfferOverview.js: Unexpected token (84:40)
83 |
> 84 | initialGetProduct = async (productId) => {
| ^
85 | const { dispatch } = this.props;
86 | await dispatch(resetOfferStateAction());
87 | dispatch(getProductAction(productId));
This is the Component, I have cut most of it's contents as I doubt they could be related to the issue:
export class OfferOverview extends Component {
componentWillMount() {
this.initialGetProduct(this.props.location.query.product_id);
}
// same error will be using async initialGetProduct() {
initialGetProduct = async (productId) => {
const { dispatch } = this.props;
await dispatch(resetOfferStateAction());
dispatch(getProductAction(productId));
this.selectProduct(productId);
}
render() { ... }
}
I have tried setting es2017 preset in babel configuration, same as using "transform-async-to-generator" plugin. This is what I have in .babelrc file:
{
"presets": [
"es2017",
"react",
"stage-0"
],
"plugins": [
"react-hot-loader/babel",
"transform-async-to-generator",
"transform-class-properties",
["import", { "libraryName": "antd", "style": "css" }]
]
}
This is my eslint configuration. On eslint discussion they said this is more babel-eslint issue although I've added parserOptions ecmaVersion = 2017:
module.exports = {
root: true,
parser: 'babel-eslint',
// import plugin is termporarily disabled, scroll below to see why
plugins: [/*'import', */'flowtype', 'jsx-a11y', 'react'],
env: {
browser: true,
commonjs: true,
node: true
},
parserOptions: {
ecmaVersion: 2017,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
generators: true,
experimentalObjectRestSpread: true
}
},
settings: {
'import/ignore': [
'node_modules',
'\\.(json|css|jpg|png|gif|eot|svg|ttf|woff|woff2|mp4|webm)$',
],
'import/extensions': ['.js'],
'import/resolver': {
node: {
extensions: ['.js', '.json']
}
}
},
rules: { ... }
};
and package json dependencies:
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.13.2",
"babel-eslint": "^6.1.2",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-runtime": "^6.12.0",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.13.2",
"babel-preset-es2016": "^6.24.1",
"babel-preset-es2017": "^6.24.1",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-0": "^6.24.1",
"coveralls": "^2.11.12",
"eslint": "^3.2.2",
"eslint-config-airbnb": "^10.0.0",
"eslint-plugin-react": "^6.0.0",
"ignore-styles": "^4.0.0",
"istanbul": "^1.0.0-alpha.2",
"mocha": "^3.0.2",
"nock": "^8.0.0",
"react-addons-test-utils": "^15.3.0",
"react-scripts": "0.2.1",
"redux-mock-store": "^1.1.2",
"redux-saga-devtools": "^0.1.2"
},
"dependencies": {
"babel-polyfill": "^6.26.0",
"enzyme": "^2.4.1",
"eslint-plugin-flowtype": "^2.39.1",
"eslint-plugin-jsx-a11y": "^6.0.2",
"expect": "latest",
"isomorphic-fetch": "^2.2.1",
"jsdom": "^9.9.1",
"lodash": "^4.17.4",
"nuka-carousel": "^2.3.0",
"pushstate-server": "latest",
"react": "^15.2.1",
"react-dom": "^15.2.1",
"react-redux": "^4.4.5",
"react-router": "^2.6.0",
"react-router-redux": "^4.0.8",
"react-sidebar": "^2.3.2",
"redux": "^3.6.0",
"redux-logger": "3.0.1",
"redux-saga": "^0.14.3"
},
Would appreciate any hint what can be wrong?
async initialGetProduct()
? It can't be the same exact error since there is no arrow. – ReyHaynesstage-2
will include presets forstage-3
andstage-4
(reference) – a stage preset will include all stages greater than itself. Also OP has the ES2017 preset which includes covers the functionality they need since it includes thetransform-async-to-generator
Babel plugin (reference). However your comment has led to this important note for OP:stage-0
is configured as a preset however dev dependencies hasstage-2
– this dependency mismatch needs to be resolved. – Wing