I'm building a simple app that searches for movies from a movie DB. The search input successfully fetchs and sets the apiData to movies with the same value.
I'm struggling to map and display the title of the movies and get the error apiData.map is not a function
Data is state data is an array of objects.
I want to access the title from the objects i.e
{title: 'mad max'} and display this.
Here's the code
const SearchPage = () => {
const [apiData, setApiData] = useState({ results: {} });
const [searched, setSearched] = useState([]);
const [loading, setLoading] = useState(false);
const handleSearch = (event) => {
setSearched(event.target.value);
};
useEffect(() => {
setLoading(true);
fetchSearched(searched).then((data) => setApiData(data.results));
setLoading(false);
}, [searched]);
return (
<>
<form className='search-form'>
<input
type='text'
placeholder='search for a film'
onChange={(event) => handleSearch(event)}
/>
</form>
// code below causes error when un-commented
{/* <SearchList apiData={apiData} /> */}
</>
);
};
const SearchList = ({ apiData }) => {
return (
<div className='search-list'>
SEARCH LIST
<ul>
{apiData.map((movie) => {
return <SearchItem movie={movie} key={movie.id} />;
})}
</ul>
</div>
const SearchItem = ({ movie }) => {
return (
<div className='search-item'>
<li>{movie.title}</li>
</div>
Mapping data from an API keeps tripping me up so any clarification would be appreciated.
apiDatato{ results: [] }? Also, when you pass inapiDatatoSearchList, do you mean to passapiData.results? Ensure that you are consistent with what you are settingapiDatato. Is it an object, whereresultis an array?