0

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
3
  • 1
    You'll need to show your code Commented Jan 9, 2023 at 22:48
  • 1
    show minimal reproduction of the problem with the code you're working with Commented Jan 9, 2023 at 22:48
  • const [items, setItems] = useState() items is not initially an array and will throw an error if you try to map over something that is not an array. Setting your state like this const [items, setItems] = useState([]) will solve the issue Commented Jan 10, 2023 at 0:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.