Building a Todo App with Zustand and TypeScript

Rind Devran Tukan
Rind Devran Tukan
< 1 min read
Todo app with Zustand
Reading Time: < 1 minute

TypeScript Setup

interface Todo {
  id: string;
  text: string;
  done: boolean;
}

interface TodoStore {
  todos: Todo[];
  addTodo: (text: string) => void;
  toggleTodo: (id: string) => void;
}

const useStore = create<TodoStore>((set) => ({
  todos: [],
  addTodo: (text) => set((state) => ({
    todos: [...state.todos, { id: Date.now().toString(), text, done: false }],
  })),
  toggleTodo: (id) => set((state) => ({
    todos: state.todos.map((todo) =>
      todo.id === id ? { ...todo, done: !todo.done } : todo
    ),
  })),
}));

UI Implementation

function TodoList() {
  const { todos, addTodo, toggleTodo } = useStore();
  const [input, setInput] = useState('');

  return (
    <div>
      <input value={input} onChange={(e) => setInput(e.target.value)} />
      <button onClick={() => addTodo(input)}>Add</button>
      {todos.map((todo) => (
        <div key={todo.id} onClick={() => toggleTodo(todo.id)}>
          {todo.text} {todo.done ? '✓' : ''}
        </div>
      ))}
    </div>
  );
}

Conclusion

A fully type-safe Todo app with Zustand and TypeScript.

Related Articles

Discover more stories from our blog

AI programming workflow integration

How to Integrate AI into Your Programming Workflow (Step-by-Step Guide)

A practical guide to integrating AI tools like GitHub Copilot and ChatGPT into your coding workflow for maximum efficiency.
< 1 min read
AI tools for learning programming

Best AI Tools for Learning to Code in 2024 (Free & Paid)

Discover the best AI-powered tools to learn programming in 2024, from free interactive tutors to premium coding assistants.
< 1 min read
AI coding assistant comparison

AI Coding Assistants in 2024: GitHub Copilot vs. ChatGPT vs. CodeWhisperer (Compared)

GitHub Copilot, ChatGPT, or CodeWhisperer? We compare the top AI coding assistants of 2024 based on speed, accuracy, and usability.
< 1 min read
AI and programming jobs future

The Future of Programming Jobs: Will AI Replace Developers? (Data-Driven Answer)

Will AI replace programmers? Data from Google Trends and job markets reveal the truth about the future of coding careers.
< 1 min read