nextjs 实现TodoList网页应用案例
创始人
2024-11-05 01:38:02
0

参考:
https://nextjs.org/

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

1、创建项目:

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

npx create-next-app@latest todolist cd todolist 

在这里插入图片描述
在这里插入图片描述

2、创建组件、页面
在这里插入图片描述

pages目录下创建index.js

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 (     

TodoList

addTodo} /> todos} toggleTodo={toggleTodo} deleteTodo={deleteTodo} />
) }

pages目录下创建_app.js

import '../styles/globals.css'  function MyApp({ Component, pageProps }) {   return ...pageProps} /> }  export default MyApp 

在components目录下创建TodoForm.js

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 (       
handleSubmit} className="todo-form"> value} onChange={(e) => setValue(e.target.value)} placeholder="Add a todo" />
) }

在components目录下创建TodoList.js:

export default function TodoList({ todos, toggleTodo, deleteTodo }) {     return (       
    {todos.map((todo, index) => (
  • () => toggleTodo(index)} /> {todo.text}
  • ))}
) }

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

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、运行

测试

npm run dev 

在这里插入图片描述
在这里插入图片描述

构建部署

npm run build 

相关内容

热门资讯

透视黑科技"德州局脚... 透视黑科技"德州局脚本"详细辅助细节方法(真是存在有挂);在进入德州局脚本辅助挂后,参与本局比赛的八...
第5分钟了解!传送屋激k辅助器... 第5分钟了解!传送屋激k辅助器下单,超级三加一控制(一贯真的是有挂)1、玩家可以在传送屋激k辅助器下...
透视肯定!wepokerplu... 透视肯定!wepokerplus辅助(透视)永久脚本辅助助手(详细辅助总结教程)该软件可以轻松地帮助...
透视教程"德普之星透... 透视教程"德普之星透视免费"详细辅助力荐教程(真是存在有挂)德普之星透视免费软件透明挂微扑克wpk插...
第六分钟了解!家乡大二辅助,钱... 第六分钟了解!家乡大二辅助,钱塘十水三攻略(果然是真的有挂);1、玩家可以在钱塘十水三攻略软件透明挂...
透视安卓版!pokerworl... 透视安卓版!pokerworld修改器(透视)永久脚本辅助方法(详细辅助技巧教程);1、很好的工具软...
透视免费"pokem... 透视免费"pokemmo辅助官网"详细辅助攻略教程(都是是真的有挂);1、pokemmo辅助官网ai...
三分钟了解!胡乐辅助脚本下载,... 三分钟了解!胡乐辅助脚本下载,打哈儿脚本(确实存在有挂)1、超多福利:超高返利,海量正版游戏,胡乐辅...
透视插件!德普之星app安卓版... 透视插件!德普之星app安卓版破解版(透视)永久脚本辅助软件(详细辅助揭秘教程)1、许多玩家不知道德...
透视神器"德州局透视... 透视神器"德州局透视脚本"详细辅助wpk教程(原来真的有挂)1、超多福利:超高返利,海量正版游戏,德...