Reputation: 30300
The render
method in the React component looks like this:
render() {
return <h1>Hello, {this.props.name}</h1>;
}
The error is this:
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: Unexpected token 20 | 21 | render() { > 22
| return <h1>Hello, {this.props.name}</h1>;
|
Here is my package.json
:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"babel-version": "babel --version"
},
"babel": {
"presets": [
"env",
"react",
"stage-2"
]
},
"author": "",
"license": "ISC",
"devDependencies": {
"ajv": "^6.0.0",
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"css-loader": "^1.0.0",
"less": "^3.8.0",
"less-loader": "^4.1.0",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"reactstrap": "^6.3.1",
"style-loader": "^0.21.0",
"webpack": "^4.16.3",
"webpack-command": "^0.4.1"
},
"dependencies": {
"bootstrap": "^4.1.3",
"npm": "^6.3.0"
}
}
Here is the whole class:
export default class SearchForm extends React.Component {
constructor(props) {
super(props);
this.state = {
name: ""
};
}
handleNameChange = (e) => {
this.setState({ name: e.target.value });
};
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Note the class doesn't make sense in terms of functionality at the moment because I've been experimenting to figure out the syntax error.
I have a .babelrc
also:
{
"plugins": [
["transform-class-properties"]
]
}
The method is pretty straightforward, so I can't see what the issue is. Any insight is appreciated.
Upvotes: 1
Views: 244
Reputation: 112777
You are most likely getting the error because the class property above (handleNameChange = (e) => { ... };
) is a stage 2 proposal.
If you install babel-preset-stage-2
and add it to your .babelrc
the error should be resolved.
.babelrc
{
"presets": [
["env", "react", "stage-2"]
]
}
Upvotes: 1