0
0
NextJSframework~20 mins

State synchronization patterns in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Sync Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does useState synchronize state across components?

Consider two sibling components in Next.js using useState independently. What happens when one updates its state?

NextJS
import React, { useState } from 'react';

function SiblingA() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Increment A: {count}</button>;
}

function SiblingB() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Increment B: {count}</button>;
}
AUpdating state in SiblingA does not affect SiblingB's state; they remain independent.
BUpdating state in SiblingA automatically updates SiblingB's state to the same value.
CBoth components share the same state because useState is global in Next.js.
DUpdating state in SiblingA causes a runtime error due to conflicting states.
Attempts:
2 left
💡 Hint

Think about how useState works inside each component instance.

state_output
intermediate
2:00remaining
What is the output when syncing state with useEffect and props?

Given a Next.js component receiving a count prop and syncing it to local state with useEffect, what will be the displayed count after prop changes?

NextJS
import React, { useState, useEffect } from 'react';

function Counter({ count }) {
  const [localCount, setLocalCount] = useState(count);
  useEffect(() => {
    setLocalCount(count);
  }, [count]);
  return <div>{localCount}</div>;
}

// Parent renders <Counter count={5} />, then updates to <Counter count={10} />
AThe component always shows 5, ignoring prop updates.
BThe component first shows 5, then updates to show 10 after prop changes.
CThe component shows 10 immediately without showing 5 first.
DThe component throws an error because state and props conflict.
Attempts:
2 left
💡 Hint

Consider how useEffect updates state when props change.

🔧 Debug
advanced
2:00remaining
Why does this Next.js component cause infinite re-renders?

Examine the code below. Why does it cause infinite re-renders?

NextJS
import React, { useState, useEffect } from 'react';

function Timer() {
  const [seconds, setSeconds] = useState(0);
  useEffect(() => {
    setSeconds(seconds + 1);
  }, [seconds]);
  return <div>{seconds}</div>;
}
ABecause <code>setSeconds</code> inside <code>useEffect</code> updates <code>seconds</code>, triggering <code>useEffect</code> again endlessly.
BBecause <code>seconds</code> is never updated, so the component freezes.
CBecause <code>useEffect</code> is missing a cleanup function causing memory leaks.
DBecause <code>useState</code> is used incorrectly without initial value.
Attempts:
2 left
💡 Hint

Think about what triggers useEffect and what happens inside it.

🧠 Conceptual
advanced
2:00remaining
Which pattern best synchronizes state between server and client in Next.js?

You want to keep a piece of state synchronized between server-rendered content and client interactions in Next.js. Which approach is best?

AUse <code>getStaticProps</code> to fetch data and update state only on the client.
BUse only client-side state with <code>useState</code> and ignore server data.
CUse global variables to share state between server and client.
DUse React Server Components to fetch data on the server and pass it as props to client components with local state.
Attempts:
2 left
💡 Hint

Think about how Next.js handles server and client rendering.

📝 Syntax
expert
3:00remaining
Which option correctly implements a shared state using React Context in Next.js?

Choose the correct code snippet that creates a React Context to share a counter state and updater across components.

NextJS
import React, { createContext, useState, useContext } from 'react';

const CounterContext = createContext();

function CounterProvider({ children }) {
  const [count, setCount] = useState(0);
  return <CounterContext.Provider value={{ count, setCount }}>{children}</CounterContext.Provider>;
}

function useCounter() {
  return useContext(CounterContext);
}
A
const CounterContext = React.createContext();

function CounterProvider({ children }) {
  const [count, setCount] = useState(0);
  return &lt;CounterContext.Provider value={count}&gt;{children}&lt;/CounterContext.Provider&gt;;
}

function useCounter() {
  return useContext(CounterContext);
}
B
const CounterContext = React.createContext(0);

function CounterProvider({ children }) {
  const count = 0;
  return &lt;CounterContext.Provider value={count}&gt;{children}&lt;/CounterContext.Provider&gt;;
}

function useCounter() {
  return useContext(CounterContext);
}
C
const CounterContext = React.createContext();

function CounterProvider({ children }) {
  const [count, setCount] = useState(0);
  return &lt;CounterContext.Provider value={{ count, setCount }}&gt;{children}&lt;/CounterContext.Provider&gt;;
}

function useCounter() {
  return useContext(CounterContext);
}
D
const CounterContext = React.createContext();

function CounterProvider({ children }) {
  const [count, setCount] = useState(0);
  return &lt;CounterContext.Provider value={setCount}&gt;{children}&lt;/CounterContext.Provider&gt;;
}

function useCounter() {
  return useContext(CounterContext);
}
Attempts:
2 left
💡 Hint

Remember the context value should provide both state and updater to consumers.