nextjs 实现TodoList网页应用案例

参考:

https://nextjs.org/

Next.js 是用于网络的一种 React 框架。一些世界上最大的公司在使用它,它能够借助 React 组件的力量让您创建高质量的网络应用程序。

1、创建项目:

另外注意:pages与app路由存在冲突,如果有app文件夹删除,比如这里创建的就用src下面app文件夹,整体删除,不然构建项目会报错

bash 复制代码
npx create-next-app@latest todolist
cd todolist


2、创建组件、页面

pages目录下创建index.js

bash 复制代码
import { useState, useEffect } from 'react'
import TodoForm from '../components/TodoForm'
import TodoList from '../components/TodoList'

export default function Home() {
  const [todos, setTodos] = useState([])

  useEffect(() => {
    const storedTodos = JSON.parse(localStorage.getItem('todos') || '[]')
    setTodos(storedTodos)
  }, [])

  useEffect(() => {
    localStorage.setItem('todos', JSON.stringify(todos))
  }, [todos])

  const addTodo = (text) => {
    setTodos([{ text, completed: false }, ...todos])
  }

  const toggleTodo = (index) => {
    const newTodos = [...todos]
    newTodos[index].completed = !newTodos[index].completed
    setTodos(newTodos)
  }

  const deleteTodo = (index) => {
    const newTodos = todos.filter((_, i) => i !== index)
    setTodos(newTodos)
  }

  return (
    <div className="container">
      <h1>TodoList</h1>
      <TodoForm addTodo={addTodo} />
      <TodoList todos={todos} toggleTodo={toggleTodo} deleteTodo={deleteTodo} />
    </div>
  )
}

pages目录下创建_app.js

bash 复制代码
import '../styles/globals.css'

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp

在components目录下创建TodoForm.js

bash 复制代码
import { useState } from 'react'

export default function TodoForm({ addTodo }) {
    const [value, setValue] = useState('')
  
    const handleSubmit = (e) => {
      e.preventDefault()
      if (!value) return
      addTodo(value)
      setValue('')
    }
  
    return (
      <form onSubmit={handleSubmit} className="todo-form">
        <input
          type="text"
          value={value}
          onChange={(e) => setValue(e.target.value)}
          placeholder="Add a todo"
        />
        <button type="submit">Add</button>
      </form>
    )
  }

在components目录下创建TodoList.js:

bash 复制代码
export default function TodoList({ todos, toggleTodo, deleteTodo }) {
    return (
      <ul className="todo-list">
        {todos.map((todo, index) => (
          <li key={index} className={`todo-item ${todo.completed ? 'completed' : ''}`}>
            <input
              type="checkbox"
              checked={todo.completed}
              onChange={() => toggleTodo(index)}
            />
            <span>{todo.text}</span>
            <button onClick={() => deleteTodo(index)}>Delete</button>
          </li>
        ))}
      </ul>
    )
  }

在styles目录下的globals.css中添加样式

bash 复制代码
body {
  font-family: Arial, sans-serif;
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
}

h1 {
  text-align: center;
}

.todo-form {
  display: flex;
  margin-bottom: 20px;
}

.todo-form input {
  flex-grow: 1;
  padding: 10px;
  font-size: 16px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
}

.todo-form button {
  padding: 10px 20px;
  font-size: 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
}

.todo-list {
  list-style-type: none;
  padding: 0;
}

.todo-item {
  display: flex;
  align-items: center;
  padding: 10px;
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  margin-bottom: 10px;
  border-radius: 4px;
}

.todo-item.completed {
  text-decoration: line-through;
  opacity: 0.6;
}

.todo-item input[type="checkbox"] {
  margin-right: 10px;
}

.todo-item button {
  margin-left: auto;
  background-color: #f44336;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
}

2、运行

测试

bash 复制代码
npm run dev


构建部署

bash 复制代码
npm run build
相关推荐
程序猿进阶1 小时前
ThreadLocal 释放的方式有哪些
java·开发语言·性能优化·架构·线程池·并发编程·threadlocal
程序者王大川1 小时前
【大数据】如何读取多个Excel文件并计算列数据的最大求和值
开发语言·python·excel·big data
Mryan20051 小时前
OpenJudge | 寻找中位数
开发语言·数据结构·c++·算法·openjudge
lizi888882 小时前
打包Python代码的常用方法实现程序exe应用
开发语言·python
好久不见的流星2 小时前
[基于 Vue CLI 5 + Vue 3 + Ant Design Vue 4 搭建项目] 04 安装 Vue CLI 5
javascript·vue.js·ecmascript
曈欣2 小时前
vue 控制组件是否显示
前端·javascript·vue.js
api茶飘香3 小时前
守护应用边界:通过反射API实现安全的输入输出过滤
java·开发语言·python·安全·django·virtualenv·pygame
杀死一只知更鸟debug3 小时前
策略模式的小记
java·开发语言·策略模式
nice666603 小时前
CSS的基本语法
java·前端·css·visual studio code
efls1113 小时前
Qt_了解Qt Creator
开发语言·qt