0
0
NextJSframework~20 mins

Zustand for client state in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Zustand Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Zustand store usage?
Consider this Zustand store and component code in a Next.js app. What will the component display when rendered?
NextJS
import create from 'zustand';

const useStore = create(set => ({
  count: 0,
  increment: () => set(state => ({ count: state.count + 1 }))
}));

export default function Counter() {
  const { count, increment } = useStore();
  return (
    <>
      <p>Count: {count}</p>
      <button onClick={increment}>Add</button>
    </>
  );
}
AThe component shows 'Count: 0' initially and increments count by 1 each time the button is clicked.
BThe component shows 'Count: 1' initially and increments count by 1 each time the button is clicked.
CThe component shows 'Count: 0' but clicking the button does not change the count.
DThe component throws a runtime error because the store is not initialized.
Attempts:
2 left
💡 Hint
Remember Zustand's create function initializes state and actions. The initial count is 0.
state_output
intermediate
2:00remaining
What is the value of count after these actions?
Given this Zustand store and sequence of actions, what is the final value of count?
NextJS
import create from 'zustand';

const useStore = create(set => ({
  count: 5,
  increment: () => set(state => ({ count: state.count + 1 })),
  reset: () => set({ count: 0 })
}));

// Actions executed in order:
// 1. increment()
// 2. increment()
// 3. reset()
// 4. increment()
A3
B0
C2
D1
Attempts:
2 left
💡 Hint
Apply each action step-by-step starting from count = 5.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a Zustand store with a toggle boolean?
You want to create a Zustand store with a boolean state 'isOpen' and a toggle function that flips its value. Which code is correct?
Aconst useStore = create(set => ({ isOpen: false, toggle: () => set(state => { isOpen = !state.isOpen }) }));
Bconst useStore = create(set => ({ isOpen: false, toggle: () => set(state => ({ isOpen: !state.isOpen })) }));
Cconst useStore = create(set => ({ isOpen: false, toggle: () => set({ isOpen: !isOpen }) }));
Dconst useStore = create({ isOpen: false, toggle: () => set(state => ({ isOpen: !state.isOpen })) });
Attempts:
2 left
💡 Hint
Remember the create function expects a function with set parameter returning an object.
🔧 Debug
advanced
2:00remaining
Why does this Zustand store not update the component on state change?
Given this store and component, why does the component not re-render when the button is clicked?
NextJS
import create from 'zustand';

const useStore = create(set => ({
  count: 0,
  increment: () => set({ count: count + 1 })
}));

export default function Counter() {
  const count = useStore(state => state.count);
  const increment = useStore(state => state.increment);
  return (
    <>
      <p>{count}</p>
      <button onClick={increment}>Add</button>
    </>
  );
}
AThe component does not subscribe to the store, so it never re-renders.
BThe set function is missing in the store creation, so state cannot update.
CThe increment function uses 'count' variable which is undefined in its scope, so state never updates.
DThe button's onClick handler is not correctly assigned, so increment is never called.
Attempts:
2 left
💡 Hint
Check how the increment function accesses the current count value.
🧠 Conceptual
expert
2:00remaining
Which statement about Zustand and Next.js client state is true?
Select the correct statement about using Zustand for client state management in a Next.js app.
AZustand stores are client-side only and should not be used to share state between server and client without hydration handling.
BZustand stores should be created inside React components to isolate state per component instance.
CZustand stores are global and persist across server and client rendering automatically without extra setup.
DZustand requires wrapping the Next.js app with a provider component to work correctly.
Attempts:
2 left
💡 Hint
Think about how Zustand manages state and Next.js rendering phases.