Consider a Next.js page that fetches data on the client side without any caching or optimization. What is the likely effect on user experience?
import React from 'react'; export default function Page() { const [data, setData] = React.useState(null); React.useEffect(() => { fetch('/api/data').then(res => res.json()).then(setData); }, []); if (!data) return <p>Loading...</p>; return <p>Data loaded: {data.value}</p>; }
Think about what happens when data is fetched only after the page loads.
Fetching data on the client side without caching causes the page to show a loading state and delays content display, making the experience feel slow.
Which of these Next.js data fetching methods helps improve initial page load by pre-rendering data?
Consider which method runs on the server before sending HTML to the browser.
getServerSideProps fetches data on the server before rendering, so the page is sent with data ready, improving load performance.
Given a Next.js component that receives props but does not use memoization, what is the likely impact on performance?
import React from 'react'; function Child({ value }) { console.log('Child rendered'); return <p>{value}</p>; } export default function Parent() { const [count, setCount] = React.useState(0); return ( <> <button onClick={() => setCount(count + 1)}>Increment</button> <Child value={count} /> </> ); }
Think about how React decides to re-render child components.
Without memoization, the Child component re-renders every time the Parent re-renders, even if props did not change, causing unnecessary work.
Examine the code below. Why might navigating to this page feel slow?
import React from 'react'; export default function Page() { const [data, setData] = React.useState(null); React.useEffect(() => { async function fetchData() { const res = await fetch('/api/large-data'); const json = await res.json(); setData(json); } fetchData(); }, []); if (!data) return <p>Loading large data...</p>; return <p>Data loaded with {data.items.length} items.</p>; }
Consider when the data is fetched and how big data affects load time.
Fetching large data on the client after page load causes a loading delay and slow navigation experience.
Next.js automatically splits code into smaller chunks. Why does this improve performance?
Think about how loading smaller files affects browser speed.
Code splitting sends only the needed JavaScript for the current page, reducing initial load size and improving speed.