Consider this React Server Component in the app/page.tsx file:
export default function Page() {
return (
<main>
<h1>Welcome to Next.js App Router</h1>
<p>This is the home page.</p>
</main>
)
}What will the browser display when visiting the root URL?
export default function Page() { return ( <main> <h1>Welcome to Next.js App Router</h1> <p>This is the home page.</p> </main> ) }
Think about what the component returns inside the main tag.
The component returns a main element containing an h1 and a p. This renders exactly as described in option A.
In the Next.js App Router, you want to create a layout that wraps all pages in a folder. Which of these code snippets correctly defines a layout component?
Layouts receive a children prop as an object property.
Option D correctly destructures children from props and returns it wrapped in a div. Option D does not accept children, so it won't wrap pages. Option D returns children directly but misses wrapping. Option D treats children as a direct parameter, which is incorrect.
Given a React Server Component in the Next.js App Router, under which condition will it re-run on the server to fetch fresh data?
Think about when server components run in Next.js App Router.
React Server Components run on the server each time the page is requested or refreshed. Client-side state changes or button clicks do not trigger server re-runs unless they cause navigation or data fetching.
Examine this app/page.tsx code:
export default function Page() {
const data = fetch('https://api.example.com/data')
return (
<div>{JSON.stringify(data)}</div>
)
}What is the cause of the runtime error?
export default async function Page() { const data = await fetch('https://api.example.com/data') return ( <div>{JSON.stringify(data)}</div> ) }
Remember that fetch returns a Promise and needs to be awaited.
fetch returns a Promise. Without awaiting it, data is a Promise object, which JSON.stringify cannot serialize properly. This causes a runtime error.
Consider this client component in app/counter/page.tsx:
'use client'
import { useState } from 'react'
export default function Counter() {
const [count, setCount] = useState(0)
function handleClick() {
setCount(count + 1)
setCount(count + 1)
}
return (
<div>
<p>Count: {count}</p>
<button onClick={handleClick}>Increment Twice</button>
</div>
)
}What will the count display after clicking the button once?
'use client' import { useState } from 'react' export default function Counter() { const [count, setCount] = useState(0) function handleClick() { setCount(count + 1) setCount(count + 1) } return ( <div> <p>Count: {count}</p> <button onClick={handleClick}>Increment Twice</button> </div> ) }
Remember how React batches state updates with the same value.
Both setCount calls use the same stale value of count (0), so the state updates to 1 only once. To increment twice, use the functional update form.