I've got "TypeError: Cannot read properties of undefined (reading 'map')", even though useState() is initialized with array. The error occurs only in one component. Other components, which, I think, are also using useState and useEffect the same way, don't resolve with this error.
import { useState, useEffect } from "react/cjs/react.development";
import * as constants from "../../../../constants";
export default function Keywords(props) {
const [movieKeywords, setMovieKeywords] = useState([]);
useEffect(() => {
const fetchKeywords = async () => {
const data = await fetch(
`${constants.TMDB_BASE_PATH}movie/${props.id}/keywords?api_key=${constants.API_KEY}`
);
const jsonData = await data.json();
setMovieKeywords(jsonData.keywords);
console.log("xdd");
};
fetchKeywords();
}, []);
return (
<div className="flex flex-wrap">
{movieKeywords.map((keyword) => {
return (
<div className="border font-oxygen m-1 rounded-xl cursor-pointer text-xs text-gray-300 px-2 py-1">
<p>{keyword.name}</p>
</div>
);
})}
</div>
);
}
I will be glad if anyone could point me in the right direction.
jsonDatainuseEffect. Maybe itsundefined.jsonData.keywordsmay beundefinedand therefore the linesetMovieKeywords(jsonData.keywords)could set yourmovieKeywordsstate variable toundefined