每当用户单击删除按钮时,我都想删除 li。所以我传递了索引,但它在我点击它之前就被触发了,我想我在传递值时做错了。
var App = React.createClass({
getInitialState(){
return {
items:[1,2,3],
isEdit: false
}
},
renderEditForm(){
return(
<div>
<input type="text" />
<button onClick={this.saveHandler}>save</button>
</div>
)
},
ItemCtrl(index){
return(
<div className="itemCtrlWrap">
<button onClick={this.editHandler}>Edit</button>
<button onClick={this.dltHandler(index)}>Delete</button>
</div>
)
},
editHandler(){
this.setState({isEdit:true})
},
saveHandler(){
this.setState({isEdit:false})
},
dltHandler(index){
console.log(index) //shall recieve index here upon click
},
renderItem(){
return(
this.state.items.map((item,i)=>
<li key={i}> {this.state.isEdit ? this.renderEditForm() : item} {this.ItemCtrl(i)}</li>)
)
},
render(){
return(
<ul>
{this.renderItem()}
</ul>
)
}
})
React.render(<App />, document.getElementById('container'));