【ReactNative】入门:从todo App开始(5)

删除一条todo item

row.js 首先引入TouchableOpacity

import { View, Text, StyleSheet, Switch, TouchableOpacity } from 'react-native';

在text的后面加上一个x

<View style={styles.textWrap}>
    <Text style={[styles.text, complete && styles.complete]}>{this.props.text}</Text>
</View>
<TouchableOpacity>
    <Text style={styles.destroy}>X</Text>
</TouchableOpacity>

给x写style

destroy: {
    fontSize: 20,
    color: "#CC9a9a"
  },

下面写具体的逻辑,实现的思路就是用filter方法,把不等于当前key的items作为新的items返回
app.js

handleRemoveItem(key) {
    const newItems = this.state.items.filter((item) => {
      return item.key !== key
    })
    this.setSource(newItems, newItems);
  }

wire up

return (
                <Row
                  key={key}
                  onRemove={() => this.handleRemoveItem(key)}
                  onComplete={(complete) => this.handleToggleComplete(key, complete)}
                  {...value}
                />
              )

Row里面加上onPress事件

<TouchableOpacity onPress={this.props.onRemove}>
          <Text style={styles.destroy}>X</Text>
</TouchableOpacity>

现在点击X已经能成功删除啦!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容