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> </> ); }
The Zustand store initializes count to 0. The increment function updates the state by adding 1. The component reads count and calls increment on button click, so the count updates correctly.
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()
Starting at 5, two increments add 2 (count = 7), reset sets count to 0, then one increment makes count 1.
Option B correctly uses create with a function taking set and returns an object with state and toggle function. The toggle uses set with a function accessing current state.
Option B passes an object directly, which is invalid. Option B uses undefined 'isOpen' variable outside state. Option B uses a block without return in set, causing no state update.
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> </> ); }
The increment function uses 'count' which is not defined inside its scope. It should use set(state => ({ count: state.count + 1 })) to access current state. Because of this, the state update does not happen and component does not re-render.
Zustand stores live on the client side and do not share state automatically between server and client. To use Zustand with Next.js, you must handle hydration carefully. Option A correctly states this.
Option A is false because Zustand does not persist state across server and client automatically. Option A is false because stores are usually created outside components to be global. Option A is false because Zustand does not require a provider.