Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Next.js Tutorial
  • Next.js Components
  • Next.js Functions
  • Next.js Deployment
  • Next.js Projects
  • Next.js Routing
  • Next.js Styles
  • Next.js Server-Side Rendering
  • Next.js Environment Variables
  • Next.js Middleware
  • Next.js Typescript
  • Next.js Image Optimization
  • Next.js Data Fetching
Open In App
Next Article:
Document Management System using NextJS
Next article icon

How to Create Todo App using Next.js ?

Last Updated : 05 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will create a to-do application and understand the basics of Next.js. This to-do list can add new tasks we can also delete the tasks by clicking on them.

Next.js is a widely recognized React framework that e­nables server-side­ rendering and enhance­s the developme­nt of interactive user inte­rfaces. With its powerful capabilities for creating performant and SEO-friendly applications, Next.js has become an ideal choice for our ToDo app.

Prerequisites:

  • Introduction To Next.js
  • Next.js Components
  • React useState
  • Javascript Map
  • NPM and NPX

Let's have a look at what the completed application will look like:

Create-Todo-Next

Steps to create the NextJS Application

Step 1: Create a new Next.js project using the following command

NPX: It is a package runner tool that comes with npm 5.2+, npx is easy to use CLI tool. The npx is used for executing Node packages.

npx create-next-app todo-app

Step 2: Change to the project directory:

cd todo-app

Project Structure:

Next-js-Project-Structure

Approach

The functions update­Input, addItem, delete­Item, and editItem are­ responsible for managing the state­ based on user actions. Specifically, the­ updateInput function updates the use­rInput state wheneve­r the user types in the­ input field. On the other hand, the­ addItem function adds a new ToDo item to the­ list state if there's conte­nt in the input field. If the use­r clicks on the "Delete­" button, it triggers the dele­teItem function which remove­s a ToDo item from the list state. Lastly, by utilizing a prompt display, the­ editItem function enable­s users to modify existing ToDo items.

Example: In this example, we will see the ToDo App using Next.js

  • index.js
JavaScript
'use client'; import React, { useState } from 'react';  const App = () => {     const [userInput, setUserInput] = useState('');     const [list, setList] = useState([]);     const [editIndex, setEditIndex] = useState(null); // Track index of item to edit      // Set a user input value     const updateInput = (value) => {         setUserInput(value);     };      // Add or edit item     const handleAction = () => {         if (userInput.trim() === '') return; // Avoid adding empty items          if (editIndex !== null) {             // Edit existing item             const updatedList = list.map((item, index) =>                 index === editIndex ? { ...item, value: userInput } : item             );             setList(updatedList);             setEditIndex(null); // Reset edit mode         } else {             // Add new item             const newItem = {                 id: Math.random(), // Consider using a more reliable ID generator                 value: userInput,             };             setList([...list, newItem]);         }          setUserInput(''); // Clear input field     };      // Function to delete item from list using id to delete     const deleteItem = (id) => {         const updatedList = list.filter((item) => item.id !== id);         setList(updatedList);     };      // Function to enable editing mode     const startEdit = (index) => {         setUserInput(list[index].value);         setEditIndex(index); // Set the index of the item to be edited     };      return (         <div             style={{                 fontFamily: 'Arial, sans-serif',                 maxWidth: '600px',                 margin: '0 auto',                 padding: '20px',             }}         >             <div                 style={{                     textAlign: 'center',                     fontSize: '2.5rem',                     fontWeight: 'bold',                     marginBottom: '20px',                     color: 'green',                 }}             >                 Geeksforgeeks             </div>             <div                 style={{                     textAlign: 'center',                     fontSize: '1.5rem',                     fontWeight: 'bold',                     marginBottom: '20px',                 }}             >                 TODO LIST             </div>             <div                 style={{ display: 'flex', alignItems: 'center', marginBottom: '20px' }}             >                 <input                     style={{                         fontSize: '1.2rem',                         padding: '10px',                         marginRight: '10px',                         flexGrow: '1',                         borderRadius: '4px',                         border: '1px solid #ccc',                     }}                     placeholder={editIndex !== null ? "Edit item..." : "Add item..."}                     value={userInput}                     onChange={(e) => updateInput(e.target.value)}                 />                 <button                     style={{                         fontSize: '1.2rem',                         padding: '10px 20px',                         backgroundColor: '#4caf50',                         color: 'white',                         border: 'none',                         borderRadius: '8px',                         cursor: 'pointer',                     }}                     onClick={handleAction}                 >                     {editIndex !== null ? 'Update' : 'ADD'}                 </button>             </div>             <div                 style={{ background: '#f9f9f9', padding: '20px', borderRadius: '8px' }}             >                 {list.length > 0 ? (                     list.map((item, index) => (                         <div                             key={item.id} // Use the unique id as the key                             style={{                                 display: 'flex',                                 justifyContent: 'space-between',                                 alignItems: 'center',                                 marginBottom: '10px',                             }}                         >                             <span style={{ fontSize: '1.2rem', flexGrow: '1' }}>                                 {item.value}                             </span>                             <span>                                 <button                                     style={{                                         padding: '10px',                                         backgroundColor: '#f44336',                                         color: 'white',                                         border: 'none',                                         borderRadius: '8px',                                         marginRight: '10px',                                         cursor: 'pointer',                                     }}                                     onClick={() => deleteItem(item.id)}                                 >                                     Delete                                 </button>                                 <button                                     style={{                                         padding: '10px',                                         backgroundColor: '#2196f3',                                         color: 'white',                                         border: 'none',                                         borderRadius: '8px',                                         cursor: 'pointer',                                     }}                                     onClick={() => startEdit(index)}                                 >                                     Edit                                 </button>                             </span>                         </div>                     ))                 ) : (                     <div                         style={{ textAlign: 'center', fontSize: '1.2rem', color: '#777' }}                     >                         No items in the list                     </div>                 )}             </div>         </div>     ); };  export default App; 

Step 4: To run the next.js application use the following command and then go to this URL http://localhost:3000

npm run dev

Output:

a2

Next Article
Document Management System using NextJS
author
saurabhkumarsharma05
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • Next.js

Similar Reads

  • Blogging Platform using Next JS
    In this project, we will explore the process of building The Blogging Platform with Next.js. Blogging Platform is a web application that allows users to create and publish blog posts. The platform provides a user-friendly interface for managing blog content and includes functionalities to create new
    5 min read
  • How to Create Todo App using Next.js ?
    In this article, we will create a to-do application and understand the basics of Next.js. This to-do list can add new tasks we can also delete the tasks by clicking on them. Next.js is a widely recognized React framework that e­nables server-side­ rendering and enhance­s the developme­nt of interact
    4 min read
  • Document Management System using NextJS
    The Document Management System is a web application developed using Next.js, that allows users to efficiently manage their documents. The system provides features for uploading, organizing, and retrieving documents. Users can upload documents through the web interface, which are then stored in local
    5 min read
  • Create a Quiz App with Next JS
    In this article, we’ll explore the process of building a Quiz App utilizing NextJS. The quiz app provides users with a series of multiple-choice questions with options. Users can select the option and move to the next question. At the end, the user will be able to see the analysis of the quiz. Outpu
    5 min read
  • E-commerce Dashboard with NextJS
    This project is an E-commerce Dashboard built with Next.js, providing features such as a dynamic sidebar, responsive navigation, order analytics, and most sold items of the week. It shows various features such as product analytics, order management, and user interaction. Output Preview: Prerequisite
    11 min read
  • Social Networking Platform using Next.js
    The Social Networking Platform built with NextJS is a web application that provides users the functionality to add a post, like a post, and be able to comment on it. The power of NextJS, a popular React framework for building server-side rendered (SSR) and statically generated web applications, this
    8 min read
  • URL Shortener Service with NextJS
    In this article, we will explore the process of building a URL shortener service using NextJS that takes a long URL and generates a shorter, more condensed version that redirects to the original URL when accessed. This shortened URL is typically much shorter and easier to share, especially in situat
    2 min read
  • Contact Us Form using Next.js
    Creating a Contact Us form in Next.js involves setting up a form component, handling form submissions, and potentially integrating with a backend service or API to send the form data. In this article, we will create a Contact Us Form with NextJS. Output Preview: Let’s have a look at what our final p
    6 min read
  • Blogging Platform using Next JS
    In this project, we will explore the process of building The Blogging Platform with Next.js. Blogging Platform is a web application that allows users to create and publish blog posts. The platform provides a user-friendly interface for managing blog content and includes functionalities to create new
    5 min read
  • Music Player App with Next.js and API
    In this tutorial, we'll create a Music Player App using NextJS, a React framework. Explore frontend development, API integration, and UI design to build a user-friendly app for seamless music enjoyment. Join us in harmonizing code and creativity to craft an engaging Music Player App with NextJS in t
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences