DEV Community

Syed Muhammad Ali Raza
Syed Muhammad Ali Raza

Posted on

Todo List with Next.js Beginner's Guide

Introduction:

Next.js is a popular framework for building server-side React applications. With reliable features and easy installation, it is an excellent choice for developing powerful and effective web applications. In this article, Next.js. we will focus on creating a todo list application using By following this step-by-step guide, you will gain a solid understanding of the basics of Next.js by building practical and practical projects.

Prerequisites:

Before you begin, make sure your device meets the following requirements:

  • Node.js: Next.js requires Node.js to run, so make sure you have it installed.
  • npm: Next.js uses the Node package manager npm to manage dependencies. Make sure npm is also installed.

Step 1: Build a new Next.js project

npx create-next-app todo-list
cd todo-list
Enter fullscreen mode Exit fullscreen mode

To create a new Next.js project, open your terminal and run the following command:
This will create a new directory called "todo-list" and start the Next.js project inside it.

Step 2: Create a Todo list component

Create two new files in the "Components" directory: "TodoForm.js" and "TodoList.js".
In TodoForm.js, implement the logic to add new todos. A basic template to get you started:
In TodoList.js, implement the logic to display the list of todos. Use the map function to iterate over the todo array and display each todo element. A basic template to get you started:

import React, { useState } from 'react';

const TodoForm = ({ addTodo }) => {
  const [todo, setTodo] = useState('');

  const handleSubmit = (e) => {
    e.preventDefault();
    if (todo.trim()) {
      addTodo(todo);
      setTodo('');
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        value={todo}
        onChange={(e) => setTodo(e.target.value)}
        placeholder="Add a new todo"
      />
      <button type="submit">Add</button>
    </form>
  );
};

export default TodoForm;
Enter fullscreen mode Exit fullscreen mode

Step 3: Managing Todo State in Next.js

import React, { useState } from 'react';
import TodoForm from '../components/TodoForm';
import TodoList from '../components/TodoList';

const IndexPage = () => {
  const [todos, setTodos] = useState([]);

  const addTodo = (text) => {
    const newTodo = {
      id: Math.random().toString(),
      text: text,
    };
    setTodos([...todos, newTodo]);
  };

  return (
    <div>
      <h1>Todo List</h1>
      <TodoForm addTodo={addTodo} />
      <TodoList todos={todos} />
    </div>
  );
};

export default IndexPage;

Enter fullscreen mode Exit fullscreen mode

Step 4: Start the Next.js Development Server

To open the development server, open your terminal, change to the project directory, and run the following command:
Next.js will compile your code and start the development server at http://localhost:3000.

npm run dev

Enter fullscreen mode Exit fullscreen mode

Step 5: Test the Todo List app
Open your browser and enter http://localhost:3000 to see your to-do list app in action. Try adding some todos using the form, see the list below.

The results:

Congratulations! You have successfully created a task list application using In this tutorial, we have covered the basics of Next.js, including setting up a new project, creating components, managing conditions, and rendering data.

You can further develop the application by adding features such as editing or deleting todos. Once you have a solid understanding of the basics of Next.js, you can explore its other powerful features and build more complex web applications. Happy coding!

Top comments (0)