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) => (
CodePudding user response:
The correct syntax is
backendData.users.map((user, i) => (
<p key={i}>
{user}
</p>
))
with a pair of parentheses around the (user, i) => (...)
function.