React版本的TODOlist

import React, { Component } from 'react'
import "./index.css"

export default class ToDolistThree extends Component {
    state = {
        list: [
            { id: 1, name: "HTML", status: "done" },
            { id: 2, name: "JAVA", status: "undone" },
            { id: 3, name: "JAVASCRIPT", status: "done" }
        ],
        param: "All"
    }
    add = () => {
        const val = this.myinput.value
        const { list } = this.state
        const t = { id: list.length, name: val, status: "undone" }
        list.push(t)
        this.setState({ list })
        console.log(list.length);
    }
    toggle = (e) => {
        const { list } = this.state
        const id = e.target.getAttribute("data-id")
        for (let obj of list) {
            if (obj.id === Number(id)) {
                obj.status = obj.status === "done" ? "undone" : "done"
                break
            }
        }
        this.setState({ list })
    }
    toggleBtn = (e) => {
        const param = e.target.getAttribute("data-item")
        this.setState({ param })
    }
    total() {
        const { list, param } = this.state
        switch (param) {
            case "Active": return list.filter(item => item.status === "done")
            case "Done": return list.filter(item => item.status === "undone")
            default: return list
        }
    }
    render() {
        const { param } = this.state
        return (
            <div>
                <div>
                    <input ref={node => this.myinput = node} type="text" /><button onClick={this.add}>添加</button>
                </div>
                <div>
                    <ul onClick={this.toggle}>
                        {this.total().map(item => {
                            return <li data-id={item.id} key={item.id} className={item.status}>{item.name}</li>
                        })}

                    </ul>
                </div>
                <div onClick={this.toggleBtn}>
                    <button data-item="All" className={param === "All" ? "data" : ""}>全部完成</button>
                    <button data-item="Active" className={param === "Active" ? "data" : ""}>已完成</button>
                    <button data-item="Done" className={param === "Done" ? "data" : ""}>未完成</button>
                </div>
            </div>
        )
    }
}


css

.undone{
    color: black;
}
.done{
    color: red;
    text-decoration: line-through;
}

.data{
    color: white;
    background-color: red;
}

可能不是特完善,尽请谅解!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React 版本ToDo List 可以使用 React 的组件化开发方式来实现。首先,你需要创建一个名为 `TodoList` 的 React 组件,它将包含待办事项列表并管理其状态。 ```jsx import React, { useState } from 'react'; const TodoList = () => { const [todos, setTodos] = useState([]); const [inputValue, setInputValue] = useState(''); const addTodo = () => { if (inputValue.trim() !== '') { setTodos([...todos, inputValue]); setInputValue(''); } }; const removeTodo = (index) => { const updatedTodos = todos.filter((_, i) => i !== index); setTodos(updatedTodos); }; return ( <div> <input type="text" value={inputValue} onChange={(e) => setInputValue(e.target.value)} /> <button onClick={addTodo}>Add</button> <ul> {todos.map((todo, index) => ( <li key={index}> {todo} <button onClick={() => removeTodo(index)}>Remove</button> </li> ))} </ul> </div> ); }; export default TodoList; ``` 在上面的代码中,我们使用了 React 的 `useState` 钩子来创建了 `todos` 和 `inputValue` 两个状态。`todos` 用于存储待办事项列表,`inputValue` 用于获取输入框的值。 `addTodo` 函数用于将输入框的内容添加到 `todos` 列表中,并清空输入框的值。`removeTodo` 函数用于移除指定位置的待办事项。 在组件的返回部分,我们渲染了一个输入框、一个添加按钮和一个待办事项列表。通过 `map` 方法遍历 `todos` 列表,并为每一个待办事项创建一个包含删除按钮的列表项。 你可以将这个 `TodoList` 组件嵌入到你的 React 应用中,从而实现一个基本的 ToDo List 功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值