Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  19] [ 1]  / answers: 1 / hits: 23642  / 3 Years ago, mon, march 1, 2021, 12:00:00

I am new with reactjs. I read similar error post, Syntax Error: Support for the experimental syntax 'jsx' isn't currently enabled
but can't solve my problem


Whe I run npm run dev


I have an error


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.

I read this text Add @babel/preset-react but I don't understand what should i do


webpack.config.js


module.exports = {
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
};

index.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();
})
...

More From » reactjs

 Answers
3

First you need to run npm install --save-dev @babel/preset-react.


This will install @babel/preset-react package and add it to your package.json file.


Then you need to create a .babelrc file in your src folder and paste the following content in it:


{
"presets": ["@babel/preset-react"]
}

After this, you can run npm run dev again.


[#50374] Friday, February 12, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kenyonmasonc

Total Points: 44
Total Questions: 117
Total Answers: 116

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
;