How to Build a React Todo App with TypeScript: Step-by-Step

Written by

in

How to Build a React Todo App with TypeScript: Step-by-Step

TL;DR: Initialize a React project with TypeScript using Vite, define interfaces for your data, and implement state management with hooks. This guide walks you through setting up components, handling forms, and ensuring type safety for a robust todo application.

Step 1: Initialize the Project

Start by opening your terminal and running the command npm create vite@latest todo-app -- --template react-ts. Navigate into the new directory with cd todo-app and install dependencies using npm install. This sets up the foundation with pre-configured TypeScript settings, ensuring you have a clean environment to start coding without manual configuration errors.

If you want to dig deeper, check out our guide on 7 AI Coding Assistant Trends Reshaping Developer Workflows.

Step 2: Define Data Types

Create a new file named types.ts in the root of your source folder. Define an interface for the Todo item to enforce data consistency. A typical structure includes an id (number), text (string), and completed (boolean). By explicitly typing your data, TypeScript will catch potential bugs early, such as passing a string where a boolean is expected, which significantly improves code reliability and maintainability during development.

Step 3: Create the Main App Component

Open App.tsx and replace the default content. Initialize state using useState from React. Create an array state for todos and a string state for the input text. Ensure the state is typed correctly, for example, useState<Todo[]>([]) and useState<string>(''). This step establishes the core logic of your application, allowing you to manage the list of tasks and the current user input efficiently.

Step 4: Implement the Form

Build a simple form inside the App component. Add an input element bound to your text state via the value and onChange properties. Create a submit function that prevents the default form behavior, adds a new Todo object to the state array, and clears the input field. Use Crypto.randomUUID() or a simple incrementing counter to generate unique IDs for each todo item. This ensures that each list item can be uniquely identified for updates or deletions.

Step 5: Display and Manage Todos

Render the list of todos by mapping over the state array. For each item, display the text and indicate its completion status. Add buttons to toggle the completed status and to delete items. When deleting, filter the existing array to remove the item with the matching ID. When toggling, map the array to update the specific item’s status. This interactive layer allows users to fully manage their tasks, completing the core functionality of a todo app.

Step 6: Add Styling

Import a CSS file into App.tsx to style the application. Add basic styles for the container, list items, and buttons. Ensure that completed items have a visual distinction, such as a strikethrough text. Clean styling enhances user experience and makes the application feel professional. Keep the styles modular by separating component-specific styles into their own files if the application grows in complexity.

Pro Tips

Always run npm run lint to catch potential issues before deploying. Consider using a library like Zustand or Redux if your app grows beyond a simple list. Keep your components small and focused for better readability. Use strict TypeScript settings to maximize the benefits of static type checking.

FAQ

Q: Why use TypeScript for a simple app?
A: TypeScript provides static type checking, which reduces runtime errors and improves code documentation, making the codebase easier to maintain and scale.

Q: Can I use CSS Modules instead of plain CSS?
A: Yes

Related Articles

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *