useEffect throws "Cannot read properties of undefined" error in Next.js 14 project #2188
-
QuestionI'm working on a Next.js 14 project using React 18, and trying to update component state based on fetched data inside a useEffect hook. However, I'm getting the following error: javascript How to reproduce (optional)What do you need help with? Expected behavior (optional)Additional information export default function Dashboard() { useEffect(() => { useEffect(() => { return ( Dashboard
{items.map((name, index) => ( ))} ); } TypeError: Cannot read properties of undefined (reading 'map') CLI VersionTypeError: Cannot read properties of undefined (reading 'map') Information about Qlty configuration and environment (optional)TypeError: Cannot read properties of undefined (reading 'map') |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Suggested Fix (Temporary Workaround): tsx |
Beta Was this translation helpful? Give feedback.
Suggested Fix (Temporary Workaround):
Add a guard inside useEffect:
tsx
Copy
Edit
useEffect(() => {
if (data) {
setItems(data.map(item => item.name));
}
}, [data]);
Let me know if I’m missing something — open to better practices!