📖 Scenario: You are building a simple Next.js app that tracks a counter value on the client side. You want to use Zustand, a lightweight state management library, to keep the counter state and update it from different components.
🎯 Goal: Create a Zustand store to hold a count value and functions to increment and decrement the count. Then use this store in a React component to display and update the counter.
📋 What You'll Learn
Create a Zustand store with count, increment, and decrement
Use the Zustand store in a React functional component
Display the current count value
Add buttons to call increment and decrement functions
Follow Next.js and React 19+ functional component patterns
💡 Why This Matters
🌍 Real World
Zustand is used in React and Next.js apps to manage client-side state simply and efficiently without complex setup.
💼 Career
Understanding Zustand helps frontend developers build scalable React apps with clean state management, a valuable skill in modern web development.
Progress0 / 4 steps
1
Create the Zustand store
Create a Zustand store in a file called useCounterStore.js with a state variable count initialized to 0. Also add two functions: increment and decrement that increase or decrease count by 1.
NextJS
Hint
Use create from Zustand to define a store with count and two functions that update count.
2
Import the store and set up the component
In a React component file called Counter.js, import useCounterStore from ./useCounterStore.js. Create a functional component called Counter that uses useCounterStore to get the count value.
NextJS
Hint
Use the hook useCounterStore inside the component to get the count value.
3
Add buttons to update the count
Inside the Counter component, use useCounterStore to get the increment and decrement functions. Add two buttons: one calls increment on click, the other calls decrement on click.
NextJS
Hint
Get increment and decrement from the store and add buttons with onClick handlers.
4
Add accessibility and export the component
Ensure the buttons have aria-label attributes for accessibility. Export the Counter component as default. The component should show the count and two accessible buttons to change it.
NextJS
Hint
Make sure the buttons have aria-label for screen readers and the component is exported as default.
Practice
(1/5)
1. What is the main purpose of using zustand in a Next.js app?
easy
A. To manage and share client-side state easily across components
B. To handle server-side rendering automatically
C. To style components with CSS-in-JS
D. To fetch data from external APIs
Solution
Step 1: Understand Zustand's role
Zustand is a small library designed to manage state on the client side simply and clearly.
Step 2: Compare options with Zustand's purpose
Only To manage and share client-side state easily across components matches Zustand's main use: managing and sharing client state across components.
Final Answer:
To manage and share client-side state easily across components -> Option A
Quick Check:
Zustand = client state management [OK]
Hint: Zustand is for client state, not styling or server tasks [OK]
Common Mistakes:
Confusing Zustand with data fetching libraries
Thinking Zustand handles styling
Assuming Zustand manages server rendering
2. Which of the following is the correct way to create a Zustand store in Next.js?
B. Using this inside increment does not update Zustand state
C. The create function is not imported
D. The increment function should be async
Solution
Step 1: Analyze increment function behavior
Using this.count += 1 tries to mutate the object directly, which does not update Zustand's internal state.
Step 2: Understand correct state update in Zustand
Zustand requires calling set to update state immutably; direct mutation won't trigger updates.
Final Answer:
Using this inside increment does not update Zustand state -> Option B
Quick Check:
Direct mutation with this = no state update [OK]
Hint: Always use set() to update Zustand state, not this [OK]
Common Mistakes:
Mutating state directly with this
Forgetting to import create
Thinking async is needed for increment
5. You want to create a Zustand store that holds a list of tasks and a function to add a new task only if it is not empty. Which code snippet correctly implements this logic?