React State Update Issue with fetch Data

Here is a real-world debugging question asked during a JavaScript/React discussion. import { useEffect, useState } from "react"; export default function App() { const [data, setData] = useState(null); useEffect(() => { fetch("https://lnkd.in/gGnJ7utj") .then((res) => res.json()) .then((result) => { setData(result); console.log("Data:", data); }); }, []); return ( <div> <h1>{data?.title}</h1> </div> ); } ❓ Question: Why does console.log("Data:", data) print null even after calling setData(result)? Think about how React state updates work and when re-renders happen. 💡 Bonus: How would you correctly log the updated data? Drop your answers in the comments 👇 #Debugging #ReactJS #JavaScript #FrontendDevelopment #CodingInterview #ReactHooks #WebDevelopment #Developers

To view or add a comment, sign in

Explore content categories