React Native实战开发8:删除todo item
2017-02-20 本文已影响115人
Jeffrey_Hu
本教程内容和https://zhiwehu.gitbooks.io/react-native/content/ 同步更新。
删除todo item
本小节我们来实现如何删除一条todo item。首先我们需要使用TouchableOpacity控件来做一个删除按钮。
我们在row.js里,在todo item的文字后面增加一个新的删除按钮,注意TouchableOpacity
的onPress
事件,绑定的方法是从外层传递进来的。
<TouchableOpacity onPress={this.props.onRemove}>
<Text style={styles.remove}>X</Text>
</TouchableOpacity>
接下来在app.js里,我们定义这个方法,使用Array.filter方法来将需要删除的item从列表中剔除出来。
handleRemoveItem(key) {
const newItems = this.state.items.filter((item) => {
return (item.key !== key);
});
this.setSource(newItems, newItems);
}
接下来,将这个方法传给Row控件
<Row
key={key}
onRemove = {() => this.handleRemoveItem(key)}
onComplete = {(complete) => this.handleToggleComplete(key, complete)}
{...value}
/>
运行结果如下:![](https://zhiwehu.gitbooks.io/react-native/content/assets/delete todo item.png)
method.bind(this)
也许你注意到了,我在app.js的App类的构造方法里,将我们定义的方法,通过bind(this)来重新设置了一下,为什么我们需要这样做呢?
this.setSource = this.setSource.bind(this);
this.handleAddItem = this.handleAddItem.bind(this);
this.handleToggleComplete = this.handleToggleComplete.bind(this);
this.handleRemoveItem = this.handleRemoveItem.bind(this);
比如我们今天的方法handleRemoveItem
是App这个类的方法,在JavaScript中,类方法是默认不绑定的,所以在handleRemoveItem
这个类方法里,如果我们不将this绑定的话,我们便无法在这个方法里使用this
,否则会报this
会undefined
。
建议参考这两篇文章:
- 官网介绍:https://facebook.github.io/react/docs/handling-events.html
- React Binding Patterns: 5 Approaches for Handling
this
代码:https://github.com/zhiwehu/todo/tree/remove