javascript - .map() 函数并使用 ReactJS 从表中删除一行

标签 javascript reactjs

我在理解 .map() 函数时遇到了问题,因为它与 ReactJS 相关。实际上,我有一个可以在其中添加行的表,但是通过传递行索引来删除行是行不通的。这就是我所拥有的;谁能澄清我做错了什么?

import React from 'react';
import { render } from 'react-dom';

class CommentList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            comments: []
        };
        this.handleCommentDelete = this.handleCommentDelete.bind(this);
    }
    handleCommentDelete(i) {
        alert('i = ' + i);
        let comments = [...this.state.comments];
        comments.splice(i, 1);
        this.setState({
            comments: comments
        });
    }
    render() {
        return (
            <table className="commentList">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    {
                        this.props.data.map((comment, i) => {
                            return (
                                <tr className="comment" key={i}>
                                    <td className="commentId">{comment.Id}</td>
                                    <td className="commentName">{comment.Name}</td>
                                    <td className="commentPhone">{comment.Phone}</td>
                                    <td className="commentEmail">{comment.Email}</td>
                                    <td className="commentCRUD">
                                        <a onClick={(i) => this.handleCommentDelete(i)}>
                                            <i className="fa fa-trash" />
                                        </a>
                                    </td>
                                </tr>
                            );
                        })
                    }
                </tbody>
            </table>
        );
    }
}

export default CommentList;

提前致谢!

最佳答案

您正在传递索引i,但方式不正确。另外,我更愿意传递id而不是index。以下是您可以执行此操作的方法:

import React from 'react';
import { render } from 'react-dom';

class CommentList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            comments: []
        };
        this.handleCommentDelete = this.handleCommentDelete.bind(this);
    }
    handleCommentDelete(id) {
        let comments = this.state.comments.filter(comment => comment.id !== id);
        this.setState({
            comments: comments
        });
    }
    render() {
        return (
            <table className="commentList">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    {
                        this.props.data.map(comment => {
                            return (
                                <tr className="comment" key={comment.Id}>
                                    <td className="commentId">{comment.Id}</td>
                                    <td className="commentName">{comment.Name}</td>
                                    <td className="commentPhone">{comment.Phone}</td>
                                    <td className="commentEmail">{comment.Email}</td>
                                    <td className="commentCRUD">
                                        <a onClick={() => this.handleCommentDelete(comment.Id)}>
                                            <i className="fa fa-trash" />
                                        </a>
                                    </td>
                                </tr>
                            );
                        })
                    }
                </tbody>
            </table>
        );
    }
}

export default CommentList;

希望这对您有用。

关于javascript - .map() 函数并使用 ReactJS 从表中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60307793/

相关文章:

javascript - 如何通过代码清理使动态导入/需要依赖于变量?

javascript - 单击当前组件时删除类名称同级组件,并在当前组件中进行切换以删除其自身的类

javascript - 即使可以通过控制台记录属性,Gatsby 应用程序也会崩溃并出现错误 'Cannot read property'

unit-testing - 使用 System.js 处理 css 导入的 React 组件的 Mocha 测试

javascript - jQuery 预置文本区域

javascript - Highchart 自定义标签似乎不适用于数据 :image/png;base64

javascript - 动态更改厚框弹出窗口大小

javascript - 将js文件链接到hbs文件

javascript - 使两个CSS类相等

reactjs - React JS 中的 ComponentWillMount 和 ComponentDidMount 有什么区别?