简体   繁体   中英

Module build failed (from ./node_modules/babel-loader/lib/index.js)

I have this code on my App.js:

import React, {useEffect, useState} from 'react'

function App() {

const [backendData, setBackendData] = useState([{}])

useEffect(() => {
fetch("/api").then(response => response.json()).then(
  data => {
    setBackendData(data)
  }
)
}, [])
 return (
  <div>
  {(typeof backendData.users === 'undefined') ? (
    <p>Loading...</p>
  ): (
    backendData.users.map(user, i) => (
      <p key={i}>
      {user}
    </p>
    ))
  
)}
</div>
)
}

 export default App

And i keep receiving this error:

ERROR in ./src/App.js

Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: E:\Interconnect\client\src\App.js: Unexpected token, expected "," (19:39)

THE LINE 19 is this one:

backendData.users.map(user, i) => (

The correct syntax is

backendData.users.map((user, i) => (
  <p key={i}>
    {user}
  </p>
))

with a pair of parentheses around the (user, i) => (...) function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM