javascript - 为什么分配了错误的索引?

标签 javascript reactjs

代码:

    handleDeleteClick: function(index) {
        var newRecipesArray = this.state.recipesArray;
        newRecipesArray.splice(index-1,1);
        this.setState({
            recipesArray: newRecipesArray
        });
    },
    render: function() {
        var i = 0;
        var that = this;

        var recipes = this.state.recipesArray.map(function(item) {
            i++
            return (
                <div key={"div"+i} className="table">
                    <Recipe key={i} name={item.name} ingredients={item.ingredients} />
                    <button key ={"edit"+i} onClick={() => { that.handleEditClick(i)}} className="btn edit btn-primary">Edit</button>
                    <button  key ={"delete"+i} onClick={() => { that.handleDeleteClick(i)}} className="btn delete btn-danger">Delete</button>
                </div>
            );
        });

        return (
            <div>
                <h1>React.js Recipe Box</h1>
                <button className="btn btn-primary" onClick={this.handleClick}>Add Recipe</button>
                <table>
                    <tbody>
                        <tr>
                            <th>RECIPES</th>
                        </tr>
                    </tbody>
                </table>
                {recipes}
                { this.state.adding ? <AddRecipe handleClose={this.handleClose}  handleAdd={this.handleAdd} /> : null }
                { this.state.editing ? <EditRecipe currentIndex = {this.state.currentIndex} handleClose={this.handleClose}  handleEdit={this.handleEdit}/> : null }
            </div>
        );
    },
});
<小时/>

情况:

当我单击“删除”时,删除的始终是最后一个元素,而不是应链接到该特定删除按钮的元素。

错误在哪里以及如何修复?

最佳答案

Map 将索引作为第二个参数,因此不需要单独的索引变量。此外,您不需要所有元素的键,而只需要父元素

handleDeleteClick: function(index) {
        var newRecipesArray = this.state.recipesArray;
        newRecipesArray.splice(index,1);
        this.setState({
            recipesArray: newRecipesArray
        });
    },
    render: function() {

        var that = this;

        var recipes = this.state.recipesArray.map(function(item, i) {

            return (
                <div key={"div"+i} className="table">
                    <Recipe name={item.name} ingredients={item.ingredients} />
                    <button onClick={() => { that.handleEditClick(i)}} className="btn edit btn-primary">Edit</button>
                    <button  onClick={() => { that.handleDeleteClick(i)}} className="btn delete btn-danger">Delete</button>
                </div>
            );
        });

关于javascript - 为什么分配了错误的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43493274/

相关文章:

javascript - 在 Ruby on Rails 中,如何使 HTML 作为一个文件中的部分内容和另一个文件中的 Javascript 最后包含?

ReactJS:停止自动提交输入

javascript - 将参数传递给 React JS 中的另一个页面

javascript - 我应该在哪里放置与 redux 中的 Action 相关的同步副作用?

javascript - 如何使用 JavaScript 将数据插入 Yii 框架 2.0 CKEditor 扩展?

javascript - Fabric.js指定lineCap样式

javascript - 如何在 JSX 中打印 props 对象,我收到 TypeError : Cannot read property of undefined

javascript - 如何将 Prop 传递给事件处理程序

javascript - 重新渲染次数过多

javascript - 允许并配置访问控制允许来源