Tillitsdone
down Scroll to discover

Building a Simple To-Do App with Zustand

Learn how to create a streamlined to-do application using Zustand, a minimalist state management solution for React.

Discover the power of simple, efficient state handling.
thumbnail

Building a Simple To-Do App with Zustand

Abstract futuristic interior with floating geometric shapes clean lines and open space. Bright golden sunlight streaming through large windows. Color palette: warm whites soft beiges and touches of amber gold. Shot from a low angle perspective looking upward to emphasize scale and floating elements high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Ever felt overwhelmed by Redux’s boilerplate code? Meet Zustand – the tiny, fast, and scaleable state management solution that’s taking the React world by storm. Today, we’ll build a simple to-do app that showcases just how easy state management can be.

Why Zustand?

Before we dive in, let’s talk about why Zustand might be your next favorite state management library. Unlike Redux, Zustand follows a minimalistic approach – no reducers, no action types, no dispatch. Just pure, simple state management that feels natural to use.

Abstract flowing cloud formations with streaming light rays piercing through layers. Color palette: crisp whites silver greys and soft sky blues. Captured from a diagonal perspective showing depth and movement high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Setting Up Our Project

First things first, let’s create a new React project and install Zustand:

Terminal window
npm create vite@latest todo-zustand -- --template react
cd todo-zustand
npm install zustand

Creating Our Store

The beauty of Zustand lies in its simplicity. Here’s how we create our todo store:

import create from 'zustand'
const useStore = create((set) => ({
todos: [],
addTodo: (text) => set((state) => ({
todos: [...state.todos, { id: Date.now(), text, completed: false }]
})),
toggleTodo: (id) => set((state) => ({
todos: state.todos.map(todo =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
)
})),
removeTodo: (id) => set((state) => ({
todos: state.todos.filter(todo => todo.id !== id)
}))
}))

Modern space station interior with sleek metallic surfaces and orbital window views. Color palette: brushed aluminum silvers cool greys and stark whites. Shot from a wide-angle perspective showing the curved architecture high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Building the Components

Now, let’s create our todo components. The great thing about Zustand is how cleanly it integrates with React components:

function TodoApp() {
const { todos, addTodo, toggleTodo, removeTodo } = useStore()
const [text, setText] = useState('')
const handleSubmit = (e) => {
e.preventDefault()
if (!text.trim()) return
addTodo(text)
setText('')
}
return (
<div>
<form onSubmit={handleSubmit}>
<input
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="What needs to be done?"
/>
<button type="submit">Add Todo</button>
</form>
<ul>
{todos.map(todo => (
<li key={todo.id}>
<input
type="checkbox"
checked={todo.completed}
onChange={() => toggleTodo(todo.id)}
/>
<span style={{ textDecoration: todo.completed ? 'line-through' : 'none' }}>
{todo.text}
</span>
<button onClick={() => removeTodo(todo.id)}>Delete</button>
</li>
))}
</ul>
</div>
)
}

What Makes This Approach Special?

  1. No Context Provider Needed: Unlike Redux or React Context, you don’t need to wrap your app in any providers.
  2. Automatic Re-rendering: Zustand only re-renders components when their specific subscribed state changes.
  3. TypeScript Ready: Zustand works great with TypeScript out of the box.
  4. DevTools Support: You can use Redux DevTools to debug your Zustand store.

Performance Considerations

Zustand is incredibly lightweight and performs really well out of the box. However, here are some tips to make your app even more efficient:

  1. Use selectors to subscribe to specific parts of the state
  2. Implement memo when needed for complex components
  3. Split your store into multiple stores if it grows too large

Conclusion

Building a todo app with Zustand shows just how straightforward modern state management can be. No complex setups, no confusing patterns – just simple, effective state management that gets out of your way and lets you focus on building features.

Abstract stone texture formation with intricate layers and natural patterns. Color palette: earthy beiges warm sandstone yellows and natural limestone whites. Photographed from a macro close-up angle to show texturing detail high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.