Building a Simple To-Do List App with React
Learn how to create a basic to-do list application using React, including setting up the project, creating components, and handling user input.
Introduction
To get started with this tutorial, you'll need to have Node.js and npm installed on your computer.
Step 1: Set up the Project
First, create a new React project using create-react-app by running the following command in your terminal:
npx create-react-app todo-list
Then, navigate into the project directory:
cd todo-list
Step 2: Create the To-Do List Component
Create a new file called TodoList.js in the src directory and add the following code:
import React, { useState } from 'react';
function TodoList() {
const [todos, setTodos] = useState([]);
const [newTodo, setNewTodo] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
setTodos([...todos, newTodo]);
setNewTodo('');
};
return (
<div>
<h1>To-Do List</h1>
<form onSubmit={handleSubmit}>
<input type='text' value={newTodo} onChange={(e) => setNewTodo(e.target.value)} />
<button type='submit'>Add Todo</button>
</form>
<ul>
{todos.map((todo, index) => (
<li key={index}>{todo}</li>
))}
</ul>
</div>
);
}
export default TodoList;
Step 3: Render the To-Do List Component
Finally, update the App.js file to render the TodoList component:
import React from 'react';
import TodoList from './TodoList';
function App() {
return (
<div>
<TodoList />
</div>
);
}
export default App;
Now you can run the application using npm start and view it in your web browser.
Ready for more? These paid resources pick up where this lesson leaves off.
Project-based React video courses — the perfect paid next step after these free lessons.
Browse on Udemy →Hand-picked React books to master the fundamentals offline.
See on Amazon →Some links on this page are affiliate links: we may earn a commission at no extra cost to you. We only recommend tools we believe are genuinely useful.