Challenge - 5 Problems
Shared State Mastery in Next.js
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does shared state behave across nested layouts in Next.js 14?
Consider a Next.js 14 app with a root layout and a nested layout. Both layouts use React's useState to hold a counter. If the nested layout updates its counter, what happens to the root layout's counter?
NextJS
import React from 'react'; export default function RootLayout({ children }) { const [count, setCount] = React.useState(0); return <html><body>{children}</body></html>; } export function NestedLayout({ children }) { const [count, setCount] = React.useState(0); // Button to increment count return <section>{children}</section>; }
Attempts:
2 left
💡 Hint
Think about how React state works in separate components.
✗ Incorrect
Each layout component has its own independent state. Updating state in the nested layout does not affect the root layout's state because React state is local to the component instance.
🧠 Conceptual
intermediate2:00remaining
What is the recommended way to share state across layouts in Next.js 14?
You want to share a theme setting (dark/light) across multiple layouts in a Next.js 14 app. Which approach correctly shares this state?
Attempts:
2 left
💡 Hint
Think about React's way to share data deeply without prop drilling.
✗ Incorrect
React Context allows sharing state across component trees, including layouts, without passing props manually.
🔧 Debug
advanced2:00remaining
Why does the shared state reset when navigating between layouts in Next.js 14?
You implemented a React Context Provider in the root layout to share state. However, when navigating between pages with different nested layouts, the shared state resets unexpectedly. What is the likely cause?
Attempts:
2 left
💡 Hint
Consider how Next.js handles layouts and navigation.
✗ Incorrect
In Next.js 14, layouts can be remounted on navigation if they are not shared between routes, causing state reset.
📝 Syntax
advanced2:00remaining
Which code snippet correctly implements a shared state using React Context in Next.js 14 layouts?
Select the code snippet that correctly creates and uses a React Context Provider in the root layout and consumes it in a nested layout.
Attempts:
2 left
💡 Hint
Check how the context value is passed and destructured.
✗ Incorrect
Option C correctly passes an object with both theme and setTheme, and the nested layout destructures theme from context.
❓ state_output
expert2:00remaining
What is the output after clicking the increment button twice in this shared state example across layouts?
Given the root layout provides a counter state via React Context, and the nested layout renders a button to increment it, what is the displayed counter value after clicking the button twice?
NextJS
import React from 'react'; const CounterContext = React.createContext(); export default function RootLayout({ children }) { const [count, setCount] = React.useState(0); return ( <CounterContext.Provider value={{ count, setCount }}> <html><body>{children}</body></html> </CounterContext.Provider> ); } export function NestedLayout({ children }) { const { count, setCount } = React.useContext(CounterContext); return ( <section> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment</button> {children} </section> ); }
Attempts:
2 left
💡 Hint
Each click increases count by 1 starting from zero.
✗ Incorrect
The initial count is 0. Each button click increments count by 1. After two clicks, count is 2.