How to create a simple to-do app in React
The rapid evolution of web technologies has reshaped the landscape of digital experiences, demanding developers to adapt and embrace new tools that can translate their creativity into user-friendly applications. One such technology is React, which has emerged as a popular JavaScript library for creating dynamic and interactive user interfaces. We can use it to create phenomenal web applications. Starting with simpler projects first enables us to grasp the fundamentals of web technology more effectively, and building a simple to-do application is a great start.
Building to-do application
Let’s look at how to incrementally create a simple to-do application that implements the
Add new tasks.
Read all the tasks.
Edit any of the existing tasks.
Delete any of the existing tasks.
Application setup
To begin, we can create a React project using the following command:
npx create-react-app todo-app
This will create a new todo-app named directory in the current working directory. We can navigate into this directory and start working on our React application. There’ll be a variety of files and folders, including the main application file, src/index.js. We can keep the ones that we need and delete the rest. To store the components that we would create for our to-do app, we create a components named directory inside the src directory.
Application structure
A simple React application typically consists of multiple
src/App.js: This is the main component of the application and serves as its entry point. It holds the state of the to-do tasks and manages their addition, removal, and update status. Additionally, it renders other components, such asTodoFormandTodoList, to provide a complete user interface for interacting with the app.src/components/ToDoForm.js: This component contains an input field and a button for adding a new task. It handles user input and triggers theaddTaskfunction when the form is submitted.src/components/ToDoList.js: This component receives the list of tasks and renders them with buttons to edit or delete them. It handles user interactions for editing or deleting tasks by calling theupdateTask()anddeleteTask()functions.
Add tasks
Once we have the project set up, we can start with the first functionality of our app, i.e., adding the tasks to our to-do list, which demonstrates the “create” operation of CRUD.
Here’s how to do it:
import React, { useState } from 'react';import './App.css';import TodoForm from './components/ToDoForm';function App() {const [tasks, setTasks] = useState([]);const addTask = (task) => {setTasks([...tasks, task]);};return (<div className="App"><h1>My To-Do App</h1><TodoForm addTask={addTask} />// Rest of the code implementation</div>);}export default App;
Code explanation:
In the code widget given above:
Line 6: We use React’s
useStatehook to manage the state of an array variable,tasks, in a functional component. Here,tasksstores the array of all the tasks the user has created using our app, andsetTasks()is a function that allows us to update the state oftasks. When we make thesetTasks(newValue)function call, React rerenders the component with the new value andtasksis updated tonewValue. We’ve initializedtasksas an empty array.
Note: You can learn more about the
useStatehook in the React hooks introduction - useState answer.
Lines 8 –10:
addTask()takes a single argument,task, and adds it to thetasksarray using thesetTasks()function.Line 15: The
ToDoFormcomponent is rendered here. We passaddTaskto this component, which then triggers it when, for example, a user submits a form to add a new task. Here, the user’s input value becomes the argument that goes intoaddTask.
Note: The complete code for all the components can be found in the example application given at the end of the Answer.
Display tasks
Because users can now add tasks using our application, we need to add the functionality to read those tasks. This demonstrates the “read” operation of CRUD. The component ToDoList has the implementation of this functionality.
Here’s how to do it:
import './App.css';import TodoForm from './components/ToDoForm';import ToDoList from './components/ToDoList';function App() {// Some codereturn (<div className="App"><h1>My To-Do App 📝</h1><TodoForm addTask={addTask} /><ul>{tasks.map((task, index) => (<ToDoListindex={index}task={task}updateTask={updateTask}deleteTask={deleteTask}/>))}</ul></div>);}export default App;
Code explanation:
Line 13:
tasks.mapiterates over each task we have in our application up till now and renders theToDoListcomponent for each of them.Line 14–18: The
index,task,updateTask, anddeleteTaskprops are used to provide data and functions to eachToDoListcomponent, allowing for dynamic rendering and interaction with the individual tasks.
Edit tasks
Let’s see how we can edit any of the already existing tasks. This functionality represents the “update” operation of CRUD. The function responsible for updating the final state of tasks is in the main component.
Here’s how to do it:
function App() {// Some codeconst updateTask = (index, updatedTask) => {const newTasks = tasks.map((task, i) =>i === index ? updatedTask : task);setTasks(newTasks);};return (// Some code);}export default App;
Code explanation:
Line 4: The
updateTask()function takes in two arguments: index (index) of the task being updated andupdatedTask, which is the new value we want to replace the existing task with. This value is taken by the user once they enter the new value for an existing task in a form placed in theToDoListcomponent and is passed toupdateTask()function along with the existing task’s index.Lines 5–7: Inside the
updateTaskfunction, we iterate over each task in thetasksarray and look for the task that has the same index value asindex. Once found, we replace its value withupdatedTask.Line 8: We use the
setTasks()function to update the state oftasksarray.
Delete tasks
Let’s look at the “delete” operation of CRUD. To demonstrate this operation, let’s add the functionality of deleting any existing tasks in our app. Again, the function responsible for deleting a task is present in the main component.
Here’s how to do it:
function App() {// Some codeconst deleteTask = (index) => {const newTasks = tasks.filter((_, i) => i !== index);setTasks(newTasks);};return (// Some code);}export default App;
Code explanation:
Line 4: The
deleteTask()function takes a single argument,index, which represents the index of the task in thetasksarray that the user wants to delete. This function gets triggered when the user clicks the delete button whose code is present in theToDoListcomponent.Line 5: We use the
filter()method to iterate over each task in thetasksarray, and remove the task whose index is being passed as the argument.Line 6: We use
setTasks()function to update the state oftasksarray. Now, it won’t have the task that was deleted.
Example
Finally, let’s take a look at our to-do app. Click the “Run” button to execute the code.
That’s it! We’ve successfully created a simple to-do app in React. We can transform this initial project into a stepping stone for creating more complex and impressive web applications, ensuring our skills remain at the cutting edge of this dynamic field.
Free Resources