I'm currently having a problem on javascript that is: "Uncaught TypeError: Cannot read properties of undefined (reading 'map')"
I tried removing the useEffect hook, didn't work, tried put ? after array like: array?.map. Didn't work You can see it right here: `
function blabla(){
const [itemIndex,setItemIndex] = useState(1)
const [items, setItems] = useState()
useEffect(function(){
function success1(data){
setItems(data)
}
var def ={
type: "GET",
url: "http://localhost:3001/blabla",
success: success1
}
jQuery.ajax(def)
}, [])
return(
<>
<Navbar/>
<section className="items">
<div className="items-col">{
items.map(function(element){
if((itemIndex-1)%3 === 0){
setItemIndex(itemIndex+1)
return <span>{element.name}</span>
}
return null
})
}</div>
<div className="items-col">{
items.map(function(element){
if(itemIndex%3 === 2){
setItemIndex(itemIndex+1)
return <span>{element.name}</span>
}
return null
})
}</div>
<div className="items-col">{
items.map(function(element){
if(itemIndex%3 === 0){
setItemIndex(itemIndex+1)
return <span>{element.name}</span>
}
return null
})
}</div>
</section>
</>
)
}
export default blabla
const [items, setItems] = useState()items is not initially anarrayand will throw an error if you try to map over something that is not anarray. Setting your state like thisconst [items, setItems] = useState([])will solve the issue