我是 reactjs 的新手。我阅读了类似的错误帖子, Syntax Error: Support for the experimental syntax ‘jsx’ is not currently enabled but can’t solve my problem
当我运行 npm run dev
我有一个错误
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: -..../frontend/src/index.js: Support for the experimental syntax 'jsx' isn't currently enabled (40:7):
38 | render() {
39 | return (
> 40 | <ul>
| ^
41 | {this.state.data.map(contact => {
42 | return (
43 | <li key={contact.id}>
Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.
我读了这篇文章 Add @babel/preset-react 但我不明白我应该怎么做
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
};
索引.js
import React, { Component } from 'react';
import { render } from "react-dom";
class App extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
loaded: false,
placeholder: "Loading"
};
}
componentDidMount() {
fetch("api/lead")
.then(response => {
if (response.status > 400) {
return this.setState(() => {
return { placeholder: "Something went wrong!" };
});
}
return response.json();
})
...
原文由 swor 发布,翻译遵循 CC BY-SA 4.0 许可协议
首先你需要运行
npm install --save-dev @babel/preset-react
。这将安装
@babel/preset-react
包并将其添加到您的 package.json 文件中。然后,您需要在
src
文件夹中创建一个.babelrc
文件,并将以下内容粘贴到其中:在此之后,您可以再次运行
npm run dev
。