(精华)2020年7月26日 React Todolist的实现
) }}
发布日期:2021-06-29 15:08:19
浏览次数:3
分类:技术文章
本文共 4651 字,大约阅读时间需要 15 分钟。
app.js
import React from 'react'import TodoMain from './components/TodoMain';import TodoHeader from './components/TodoHeader';import TodoFooter from './components/TodoFooter';class App extends React.Component{ constructor(props){ super(props) this.state = { todos:[], isAllChecked:false } } addTodo = (todoItem)=>{ this.state.todos.push(todoItem) // this.setState({ todos:this.state.todos }) } deleteTodo = (index)=>{ this.state.todos.splice(index,1) this.setState({ todos:this.state.todos }) } changeTodoState = (index,isDone)=>{ this.state.todos[index].isDone = isDone let isAllChecked = false if(this.state.todos.every(todo=>todo.isDone)){ isAllChecked = true } this.setState({ todos:this.state.todos, isAllChecked }) } changeTodoStateAll = (isDone) =>{ this.setState({ todos:this.state.todos.map((todo)=>{ todo.isDone = isDone; return todo }), isAllChecked:isDone }) } clearDone = ()=>{ // let todos = this.state.todos.filter(todo=>!todo.isDone) this.setState({ todos, isAllChecked:false }) } render(){ const { todos,isAllChecked} = this.state; let info = { todoCount:todos.length || 0, todos, todoDoneCount:(todos && todos.filter((todo)=>todo.isDone)).length || 0, isAllChecked } return(); }}export default App;
todomain.js
import React from 'react';import TodoItem from './TodoItem';export default class TodoMain extends React.Component{ render() { const { todos,todoCount,todoDoneCount} = this.props if(todos.length == 0){ return恭喜你没有代办事项}else{ return (
- { todos.map((todo,index)=>{ return
todoitem
import React from 'react';export default class TodoItem extends React.Component{ state = { isShow:false } handelMouseOver = ()=>{ this.setState({ isShow:true }) } handelMouseOut = ()=>{ this.setState({ isShow:false }) } handelDelete = ()=>{ this.props.deleteTodo(this.props.index) } handelChange = ()=>{ let isDone = !this.props.isDone; this.props.changeTodoState(this.props.index,isDone) } render(){ const { isShow} = this.state const { text,isDone} = this.props const taskDone = isDone ? "task-done" : '' return(
todoheader.js
import React from 'react';class TodoHeader extends React.Component{ handelKeyUp = (e)=>{ // 获取input的值 if(e.keyCode == 13){ let value = e.target.value; if(!value) return false let newTodoItem = { text:value, isDone:false } e.target.value = '' // 调用父组件传过来的方法 this.props.addTodo(newTodoItem) } } render() { return() }};export default TodoHeader;React Todo
todofooter.js
import React from 'react';export default class TodoFooter extends React.Component{ handelSelectAll = (e)=>{ this.props.changeTodoStateAll(e.target.checked) } handelDeleteDone = ()=>{ this.props.clearDone() } render(){ return() }}
转载地址:https://codeboy.blog.csdn.net/article/details/107598494 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月09日 04时33分04秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
电赛 | 19年全国一等奖,北航学子回忆录。
2019-04-29
电赛 | 19年全国一等奖,北航学子回忆录(上)
2019-04-29
电赛 | 19年全国一等奖,北航学子回忆录(下)
2019-04-29
突破!台积电1nm芯片,有了新进展。
2019-04-29
一文读懂全系列树莓派!
2019-04-29
自制一个害羞的口罩,见人就闭嘴,戴着可以喝奶茶
2019-04-29
聊聊我是如何编程入门的
2019-04-29
J-Link该如何升级固件?
2019-04-29
485通信自动收发电路,历史上最详细的解释
2019-04-29
一位头发发白的神人教你怎么写程序,运维,买电脑,写文章,平面设计!
2019-04-29
「第三篇」全国电子设计竞赛,这些你必须知道的比赛细节,文末附上近十年电赛题目下载...
2019-04-29
5G小科普(漫画版,So easy!)
2019-04-29
「第四篇」电赛控制题可以准备一些什么?
2019-04-29
「第六篇」对于电赛,我们应该看重什么?
2019-04-29
树莓派翻车了
2019-04-29
这位电子工程师,你不能错过。
2019-04-29
「重磅猜题之第二篇」2019年大学生电子设计竞赛
2019-04-29
干货分享 JVM 之第 3 篇 —— Java 内存结构相关
2019-04-29
基于 Hystrix 高并发服务限流第 2 篇 —— 服务隔离(线程池隔离、信号量隔离)
2019-04-29
SpringBoot 整合 JWT 实现统一认证
2019-04-29