发现一个bug,react,事件绑定会删除你希望删除目标的上一个

//这是UI组件,只负责渲染页面,不负责逻辑处理
import React, { Component } from 'react';
import "antd/dist/antd.css";
import { Input, Button, List, Typography, Tag } from "antd";


const ToDoListUI = (props) => {
  return (
    <div style={{ marginTop: "10px" }}>
      <Input
        value={props.inputValue}
        placeholder="请输入电影名字"
        style={{ width: "300px" }}
        onChange={props.handleInputChange}
      />
      <Button onClick={props.handleButtonClick} type="primary">
        提交
      </Button>
      <br />
      <Tag color="lime">想看的电影愿望清单:</Tag>
      <List
        style={{ marginTop: "10px", width: "300px" }}
        bordered
        dataSource={props.list}
        renderItem={(item, index) => (
          <List.Item
            onClick={() => {
              props.handleItemDelete(index);
            }}
          >
            {item}
          </List.Item>
        )}
      />
    </div>
  );
}

//这是无状态组件:(无状态组件就是一个函数) 可以改用const定义,如上:
// class ToDoListUI extends Component {
//     render() {
//         return (
//           <div style={{ marginTop: "10px" }}>
//             <Input
//               value={this.props.inputValue}
//               placeholder="请输入电影名字"
//               style={{ width: "300px" }}
//               onChange={this.props.handleInputChange}
//             />
//             <Button onClick={this.props.handleButtonClick} type="primary">
//               提交
//             </Button>
//             <br />
//             <Tag color="lime">想看的电影愿望清单:</Tag>
//             <List
//               style={{ marginTop: "10px", width: "300px" }}
//               bordered
//               dataSource={this.props.list}
//               renderItem={(item, index) => (
//                 <List.Item
//                   onClick={(index) => {this.props.handleItemDelete(index)}}
//                 >
//                   {item}
//                 </List.Item>
//               )}
//             />
//           </div>
//         );
//     }
// }

export default ToDoListUI;
<List.Item
            onClick={() => {
              props.handleItemDelete(index);
            }}
          >

正确的核心代码,如上:

 

 

 

如果出现错误的话,就下面这样子

<List.Item
            onClick={(index) => {
              props.handleItemDelete(index);
            }}
          >

 

 

 

原理暂时没深究,感觉就是index参数不是List.Item组件的,而是他父亲组件的,所以直接写()就行,没必要传递参数index

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值