Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Build a CRUD to-Do List App and Manage Its State in React

Learn about React, including Prepare Before You Start, key uses, practical steps, common issues, and answers to frequently asked questions.

Table of Contents

Prepare Before You Start

This practical guide explains how to build a crud to-do list app and manage its state in react with clear steps and useful context. It also covers common requirements, potential problems, and the details that can help you get better results.

  • Basic knowledge of modern JavaScript operators and React's useState hook.
  • Understand how to destructure arrays and objects in JavaScript.
  • Node 16.8 or later is installed on the local machine and knows how to use a package manager like npm or yarn.

Understanding Application Status and Management

Application state refers to the condition of the current app at a given time, including app management & information, such as user input and fetching data from a database or an API (Application Programming Interface).

To understand application state, consider possible scenarios of a simple counter app, including:

  • Default state when counter is at 0.
  • The state is incremented when the counter is incremented by 1.
  • The state drops when the counter is decremented by 1.
  • The reset state when the counter returns to the default level.

A React component can subscribe to state changes. When the user interacts with such a component, actions such as clicking a button can update this state.

The following code shows a simple counter application that, by default, is used to manage the state of clicks:

const [counter, setCounter] = useState(0); return (
 );

Setup and Install

The project's repository contains two branches: starter and context. You can use the starter branch as a base to build the project from scratch or the context branch to preview the final demo.

Starter App Cloning

The starter app provides the UI you need for the final app, so you can focus on implementing the core logic. Open a terminal and run the following command to clone the starter branch of the repository to the local machine:

git clone -b starter https://github.com/makeuseofcode/Next.js-CRUD-todo-app.git

Run the following command, in the project directory to install the dependencies and launch the development server:

yarn && yarn dev

Or:

npm i && npm run dev

If all is well, the UI should appear in your browser:

Starter App Cloning - React

Deploy Logic

The Context API provides a way to manage and share state data across components without manual prop drilling.

Step 1: Create and Export Context

Create a folder src/app/context to save context files and keep the project folder well organized. In this directory, create a file todo.context.jsx that contains all the context logic for this application.

Import the createContext function from the react library and call it, storing the result in a variable:

import { createContext} from "react"; const TodoContext = createContext();

Next, create a hook useTodoContext that returns the TodoContext as an available form.

export const useTodoContext = () => useContext(TodoContext);

Step 2: Create and Manage States

To perform CRUD (create, read, update, delete) of your application, you need to create states and manage them with the Provider component.

const TodoContextProvider = ({ children }) => { const [task, setTask] = useState(""); const [tasks, setTasks] = useState([]); return {children}; }; export default TodoContextProvider;

Just before the return statement, create a handleTodoInput function that runs when the user types are in to-do . This function then updates the task state.

const handleTodoInput = (input) => setTask(input);

Add a createTask function that runs when the user submits an app to-do . This function updates the status of tasks and assigns a random ID to the new task.

const createTask = (e) => { e.preventDefault(); setTasks([ { id: Math.trunc(Math.random() * 1000 + 1), task, }, .tasks, ]); };

Create an updateTask function that maps through a list of tasks and update the task containing an ID that matches the ID of the task that was clicked.

const updateTask = (id, updateText) => setTasks(tasks.map((t) => (t.id === id ? { .t, task: updateText } : t)));

Create a deleteTask function that updates the list of tasks so that it includes all tasks that contain IDs that do not match the parameters provided.

const deleteTask = (id) => setTasks(tasks.filter((t) => t.id !== id));

Step 3: Add a State and Handler for the Provider

Now that you've created the states and written the code to manage them, you need to make the state and handler functions available to the Provider . You can provide them as an object, using the value property of the Provider component.

return ( {children} );

Step 4: Context Scope

The created Provider must contain a top-level component to make the context available to the entire application. For this to work, edit src/app/page.jsx and include the Todos component with the TodoContextProvider :

 ; ;

Step 5: Use Context in Components

Edit the file src/app/components/Todos.jsx and destructure tasks , task , handleTodoInput , and createTask via a call to the useTodoContext function.

const { task, tasks, handleTodoInput, createTask } = useTodoContext();

Now, update the form element to handle the submit event and change it to the main input field:

 createTask(e)}> handleTodoInput(e.target.value)} /> 

Step 6: Show Task in UI

You can now use the app to create and add a task to the tasks list. To update the display, you need to map over existing tasks and display them in the UI. First, create src/app/components/Todo.jsx containing a todo item.

In src/app/components/Todo.jsx , edit or delete a task by calling the updateTask and deleteTask functions created in the file src/app/context/todo.context.jsx .

import React, { useState } from "react"; import { useTodoContext } from "./context/todo.context"; const Todo = ({ task }) => { const { updateTask, deleteTask } = useTodoContext(); // isEdit state tracks when a task is in edit mode const [isEdit, setIsEdit] = useState(false); return (

{isEdit? ( updateTask(task.id, e.target.value)} /> ): ( )}

{task.task}  
 ); }; export default Todo;

To show src/app/components/Todo.jsx for each task , drill down into the src/app/components/Todos.jsx file and conditionally map over tasks right after the header tag closes.

{tasks && ( {tasks.map((task, i) => ( ))} )}

Test the application in the browser and confirm it gives the expected results.

Step 6: Show Task in UI - React

Hope the article is useful to you.

Conclusion

Understanding React makes it easier to compare options, avoid common mistakes, and apply the information in this guide more effectively. Review the relevant requirements before making changes or choosing a solution.

FAQ

How do you build a crud to-do list app and manage its state in react?

Follow the steps in this guide in order, confirm the required settings or tools, and verify the result before making additional changes.

Is it safe to build a crud to-do list app and manage its state in react?

It is generally safe when you follow the recommended instructions, use trusted tools, and avoid changing settings you do not understand.

What should you do if the process does not work?

Recheck the requirements, restart the device or application when appropriate, and review each step for missed settings or compatibility issues.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.