Creating a Next.js Todo application is a great way to get familiar with Next.js, React, and modern web development practices. Below is a step-by-step guide to building a simple Todo application using Next.js, React, and local storage for persistence.
Step 1: Set Up Your Project
- Install Node.js: Ensure you have Node.js installed on your machine.
- Create a Next.js Project:
npx create-next-app@latest nextjs-todo-app
cd nextjs-todo-app
- Start the Development Server:
npm run dev
Visit http://localhost:3000 to see your app running.
Step 2: Create the Todo Component
Create a new file components/TodoList.js for the Todo functionality.
import { useState, useEffect } from 'react';
const TodoList = () => {
const [todos, setTodos] = useState([]);
const [inputValue, setInputValue] = useState('');
// Load todos from local storage on component mount
useEffect(() => {
const storedTodos = JSON.parse(localStorage.getItem('todos')) || [];
setTodos(storedTodos);
}, []);
// Save todos to local storage whenever the todos state changes
useEffect(() => {
localStorage.setItem('todos', JSON.stringify(todos));
}, [todos]);
const addTodo = () => {
if (inputValue.trim() !== '') {
setTodos([...todos, { id: Date.now(), text: inputValue, completed: false }]);
setInputValue('');
}
};
const toggleTodo = (id) => {
setTodos(
todos.map((todo) =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
)
);
};
const deleteTodo = (id) => {
setTodos(todos.filter((todo) => todo.id !== id));
};
return (
<div style={{ maxWidth: '400px', margin: '0 auto', padding: '20px' }}>
<h1>Todo App</h1>
<div style={{ display: 'flex', marginBottom: '20px' }}>
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder="Add a new todo"
style={{ flex: 1, padding: '10px' }}
/>
<button onClick={addTodo} style={{ padding: '10px' }}>
Add
</button>
</div>
<ul style={{ listStyle: 'none', padding: 0 }}>
{todos.map((todo) => (
<li
key={todo.id}
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
padding: '10px',
borderBottom: '1px solid #ccc',
textDecoration: todo.completed ? 'line-through' : 'none',
}}
>
<span onClick={() => toggleTodo(todo.id)} style={{ cursor: 'pointer' }}>
{todo.text}
</span>
<button onClick={() => deleteTodo(todo.id)} style={{ padding: '5px' }}>
Delete
</button>
</li>
))}
</ul>
</div>
);
};
export default TodoList;
Step 3: Integrate the Todo Component
Update the pages/index.js file to use the TodoList component.
import TodoList from '../components/TodoList';
export default function Home() {
return (
<div>
<TodoList />
</div>
);
}
Step 4: Add Styling (Optional)
You can use CSS modules, Tailwind CSS, or inline styles for better visuals. For simplicity, inline styles are used in the example above.
Step 5: Run Your Application
Start the development server (if not already running):
npm run dev
Visit http://localhost:3000 to see your Todo app in action.
Step 6: Deploy Your App (Optional)
Once your app is ready, you can deploy it using Vercel (the easiest way to deploy Next.js apps):
- Push your code to a GitHub repository.
- Deploy to Vercel by connecting your GitHub repo to Vercel.
Features of This Todo App
- Add new todos.
- Mark todos as completed.
- Delete todos.
- Persist todos in local storage.
Next Steps
- Add routing with
next/link and next/router.
- Use a backend API (e.g., with
fetch or axios) to store todos in a database.
- Add user authentication with NextAuth.js.
- Improve styling with a CSS framework like Tailwind CSS or Material-UI.
This is a basic implementation, but it covers the core functionality of a Todo app using Next.js. Happy coding! 🚀