I can console.log the array data but couldnt map through it. here is the code
import axios from 'axios'
import React from 'react'
import { useEffect } from 'react'
import { useState } from 'react'
const Row = (props) => {
const [movies, setMovies] = useState([])
useEffect(()=>{
axios.get(props.fetchURL).then((response) => {
setMovies(response.data.results)
})
}, [props.fetchURL])
console.log(movies);
return (
<>
<h2 className='text-gray-300 m-4'>{props.title}</h2>
<div className='relative flex items-center'>
<div id={'slider'}>
{movies.map((item) =>{
<div className='w-[160px] sm:w-[200px] md:w-[240px] lg:w-[280px] inline-block'>
<img src={`https://image.tmdb.org/t/p/w500/${item?.backdrop_path}`} alt={item?.title} />
<p>{item?.title}</p>
</div>
})}
</div>
</div>
</>
)
}
export default Row
can someone please tell me whats wrong with my code??
I tried rendering normal jsx elements inside the div it works fine. The problem only occurs when trying to map my array
3
Answers
You should add return.
Replace code with this:
your map function does not return anything (null array)
this is the modified one
Also I would do this
setMovies(movies => response.data.results)
to instantly set stateYou are returning undefined. You need to return the element.