尝试从状态数组从索引进行拼接,但最终从第一个索引拼接到最后一个索引
Trying to splice from state array from the index but instead end up splicing from first index to last
我试图在单击时拼接特定对象,例如我的删除功能。我已经给index分配了I的值,并复制了我的状态数组,并试图拼接它,例如arr。Splice (i, 1),但它只从第一个拼接到最后一个,例如,如果最后一个对象的移除被点击,那么第一个对象被移除,等等。我不明白为什么我使用索引不能正常工作。
var Recipes = React.createClass({
// hook up data model
getInitialState: function() {
return {
recipeList: [
{recipe: "Malek's Protein Shake", ingredients: ['1 Glass of Whole Milk', '6 tablespoons of Peanut Butter', '1 scoop of Whey', '2 Bananas', '1 scoop of Vanilla Ice Cream']},
{recipe: 'Pumpkin Pie', ingredients: ['Pumpkin Puree', 'Sweetened Condensed Milk', 'Eggs', 'Pumpkin Pie Spice', 'Pie Crust']},
{recipe: 'Spaghetti with fried eggs', ingredients: ['Noodles', 'Tomato Sauce', 'Meatballs', '4 eggs', 'Ground black pepper']}
]
}
},
ingredientList: function(ingredients) {
return ingredients.map((ingredient, i) => {
return (<li key={i} index={i} className="list-group-item">{ingredient}</li>)
})
},
eachRecipe: function(item, i) {
return (
<div key={i} index={i} className="panel panel-default">
<div className="panel-heading"><h3 className="panel-title">{item.recipe}</h3></div>
<div className="panel-body">
<ul className="list-group">
{this.ingredientList(item.ingredients)}
</ul>
<button type="button" className="btn-sm btn-info" data-toggle="modal" data-target="#myModal">Edit</button>
<button onClick={this.remove} type="button" className="btn-sm btn-danger">Remove</button>
</div>
</div>
)
},
add: function(text) {
var name = this.refs.userVal.value;
var items = this.refs.newIngredients.value.split(",");
this.setState({recipeList: [...this.state.recipeList, { recipe: name, ingredients: items }]})
},
remove: function(i) {
var arr = this.state.recipeList.slice(); //copy array
arr.splice(i, 1); //remove element
this.setState({recipeList: arr}); //update state
},
render: function() {
return (
<div>
<div>
<button type="button" className="btn btn-success btn-lg" data-toggle="modal" data-target="#myModal">Add Recipe</button>
<div id="myModal" className="modal fade" role="dialog">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<button type="button" className="close" data-dismiss="modal">×</button>
<h4 className="modal-title">Add a new recipe</h4>
</div>
<div className="modal-body">
<form role="form">
<div className="form-group">
<label forName="recipeItems">Recipe</label>
<input ref="userVal" type="recipe" className="form-control"
id="name" placeholder="Enter Recipe Name"/>
</div>
<div className="form-group">
<label for="ingredientItems">Ingredients</label>
<input ref="newIngredients" type="ingredients" className="form-control"
id="ingredients" placeholder="Enter Ingredients separated by commas"/>
</div>
<button onClick={this.add} type="button" className="btn btn-default" data-dismiss="modal">Submit</button>
</form>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div className="panelArea">
{
this.state.recipeList.map(this.eachRecipe)
}
</div>
</div>
</div>
);
}
});
ReactDOM.render(
<Recipes />,
document.getElementById('master')
)
您可以尝试在onClick事件处理程序中:
<button onClick={this.remove.bind(this, i)} type="button" className="btn-sm btn-danger">Remove</button>
相关文章:
- 名称输入的索引
- 在jQuery中获取表的行索引
- 测试索引值是否等于某个数字的倍数
- 循环遍历数组中的特定索引
- 按照选项卡索引的顺序循环一个jQuery选择
- 在JavaScript中通过索引从对象数组中获取值
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- 在索引.html和应用.js [node.js] 之间共享变量
- 如何为高图中的区域线创建z索引
- 下拉列表在使用z索引放置在前面后停止工作
- 减去两个索引不同但值相同的整数
- 在Javascript中重新建立对象数组的索引(无拼接)
- 拼接函数将相同的元素插入每个索引
- 考虑到来自另一个阵列的索引,拼接阵列中的项
- 没有索引的Javascript数组拼接
- Vue-从数组中拼接一个已定位的元素将把它的偏移量应用到下一个索引
- JS数组拼接由于某些原因没有删除索引
- 尝试从状态数组从索引进行拼接,但最终从第一个索引拼接到最后一个索引
- 在做数组拼接时如何调整索引
- 如何使用搜索或匹配自动填充拼接(索引开始、结束)