React基础教程:TodoList案例

todoList案例——增加

定义状态

// 定义状态
    state = {
        list: ["kevin", "book", "paul"]
    }

利用ul遍历list数组

<ul>
                    {
                        this.state.list.map(item =>
                            <li style={{fontWeight: "bold", fontSize: "20px"}} key={item.id}>{item.name}</li>
                        )
                    }
                </ul>

绑定点击事件,把input的值添加到list

不推荐这种写法❌

handlerClick = ()=>{
        console.log("Click4", this.myRef.current.value);

        // 不要这样写,因为不要直接修改状态,可能会造成不可预期的问题
        this.state.list.push(this.myRef.current.value);

        this.setState({
            list: this.state.list,
        })
    }

推荐这样的写法✅

handlerClick = ()=>{
        console.log("Click4", this.myRef.current.value);
    
    	// 定义一个新的数组接收
        let newList = [...this.state.list];
        newList.push(this.myRef.current.value);

        this.setState({
            list: newList
        })
    }

效果展示:

在这里插入图片描述

这里会存在一个问题,如果我插入同样的key,比如paul,这里会提示报错,提示children存在相同的key,但是这个key应该是唯一的。

在这里插入图片描述

修改方式如下:

给list加入唯一标识id

// 定义状态
 state = {
     list: [
         {
             id: 1,
             name: "kevin"
         },
         {
             id: 2,
             name: "book"
         },
         {
             id: 3,
             name: "paul"
         }]
 }

ul进行遍历的时候,绑定唯一标识符item.id

<ul>
                 {
                     this.state.list.map(item =>
                         <li style={{fontWeight: "bold", fontSize: "20px"}} key={item.id}>{item.name}</li>
                     )
                 }
             </ul>

注意在push的时候也要添加id

newList.push({
         id: Math.random()*100000000, // 生产不同的id
         name: this.myRef.current.value
     });

再次添加相同的名字,也不会报错

在这里插入图片描述

todoList案例——删除

首先给每一个li标签后,添加删除按钮

<ul>
                    {
                        this.state.list.map(item =>
                            <li style={{fontWeight: "bold", fontSize: "20px"}} key={item.id}>{item.name}
                                <Button size={"small"}
                                        style={{marginLeft:10}}
                                        type={"primary"}
                                        shape={"circle"}
                                        danger
                                        icon={<DeleteOutlined/>} />
                            </li>
                        )
                    }
                </ul>

实现效果如下:

在这里插入图片描述

接着给按钮,绑定删除事件onClick={()=>this.handlerDeleteClick(index)},并且修改列表渲染的方式,(item,index),这里的index将作为后续的额参数传递使用

<ul>
                    {
                        this.state.list.map((item, index) =>
                            <li style={{fontWeight: "bold", fontSize: "20px"}} key={item.id}>{item.name}
                                <Button size={"small"}
                                        style={{marginLeft:10}}
                                        type={"primary"}
                                        shape={"circle"}
                                        danger
                                        onClick={()=>this.handlerDeleteClick(index)}
                                        icon={<DeleteOutlined/>} />
                            </li>
                        )
                    }
                </ul>

实现handlerDeleteClick函数

handlerDeleteClick(index) {
        console.log("Del-", index);
        // 深复制
        let newList = this.state.list.concat();
        newList.splice(index, 1);
        this.setState({
            list: newList
        })
    }

实现效果如下:

在这里插入图片描述

完整的代码

注意,这里我使用了react前端的UI框架antd,大家需要自行安装使用即可。

npm install antd --save
import React, {Component} from "react";
import {Button} from 'antd';
import {DeleteOutlined} from '@ant-design/icons';

import './css/App.css'

export default class App extends Component {

    a = 35;

    myRef = React.createRef();

    // 定义状态
    state = {
        list: [
            {
                id: 1,
                name: "kevin"
            },
            {
                id: 2,
                name: "book"
            },
            {
                id: 3,
                name: "paul"
            }]
    }

    render() {
        return (
            <div style={{marginTop: 10, marginLeft: 10}}>
                <input style={{width: 200}}
                       ref={this.myRef}/>
                {/*非常推荐*/}
                <Button style={{backgroundColor: '#2ba471', border: "none"}} size={"middle"} type={"primary"}
                        onClick={() => {
                            this.handlerClick() // 非常推荐,传参数
                        }}>添加</Button>

                <ul>
                    {
                        this.state.list.map((item, index) =>
                            <li style={{fontWeight: "bold", fontSize: "20px"}} key={item.id}>{item.name}
                                <Button size={"small"}
                                        style={{marginLeft: 10}}
                                        type={"primary"}
                                        shape={"circle"}
                                        danger
                                        onClick={() => this.handlerDeleteClick(index)}
                                        icon={<DeleteOutlined/>}/>
                            </li>
                        )
                    }
                </ul>

            </div>
        )
    }

    handlerClick = () => {
        console.log("Click4", this.myRef.current.value);

        // 不要这样写,因为不要直接修改状态,可能会造成不可预期的问题
        // this.state.list.push(this.myRef.current.value);

        let newList = [...this.state.list];
        newList.push({
            id: Math.random() * 100000000, // 生产不同的id
            name: this.myRef.current.value
        });

        this.setState({
            list: newList
        })
    }

    handlerDeleteClick(index) {
        console.log("Del-", index);
        // 深复制
        let newList = this.state.list.concat();
        newList.splice(index, 1);
        this.setState({
            list: newList
        })
    }
}

相关推荐

  1. React 基础案例

    2024-06-15 03:06:06       30 阅读
  2. React框架基础教程

    2024-06-15 03:06:06       28 阅读
  3. <span style='color:red;'>TODOLIST</span>

    TODOLIST

    2024-06-15 03:06:06      24 阅读
  4. React基础教程(五):事件处理

    2024-06-15 03:06:06       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-15 03:06:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-15 03:06:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-15 03:06:06       82 阅读
  4. Python语言-面向对象

    2024-06-15 03:06:06       91 阅读

热门阅读

  1. CSS选择器种类总结

    2024-06-15 03:06:06       26 阅读
  2. 腾讯元宝APP:AIGC大模型的新篇章

    2024-06-15 03:06:06       23 阅读
  3. mysql之数据聚合

    2024-06-15 03:06:06       26 阅读
  4. 协程库——面试问题

    2024-06-15 03:06:06       30 阅读
  5. MPLS的配置

    2024-06-15 03:06:06       31 阅读
  6. Vue3Cron组件

    2024-06-15 03:06:06       22 阅读
  7. 腾讯测试开发<ieg 实验室>

    2024-06-15 03:06:06       34 阅读
  8. Python函数

    2024-06-15 03:06:06       30 阅读
  9. Superset二次开发之调研篇 v3.0 VS v4.0

    2024-06-15 03:06:06       38 阅读
  10. 深入理解Spring相关注解

    2024-06-15 03:06:06       30 阅读
  11. console-service.yaml

    2024-06-15 03:06:06       27 阅读
  12. MyBatis基础

    2024-06-15 03:06:06       31 阅读
  13. python编程技巧使用lru_cache缓存计算结果

    2024-06-15 03:06:06       54 阅读
  14. LogicFlow 学习笔记——5. LogicFlow 基础 主题 Theme

    2024-06-15 03:06:06       31 阅读