javascript - 喜欢和增量按钮在 FlatList 中不起作用
问题描述
喜欢和增量按钮在里面不起作用FlatList
这是我的构造函数、增量和类似函数:
constructor(props){
super(props);
this.state = {
count: true,
count1: 0,
};
}
onlike = () => {
this.setState(state => ({
count: !state.count
}));
}
_incrementCountNew = () => {
let newCount1 = this.state.count1 + 1
this.setState({
count1: newCount1
})
}
<FlatList
data={data}[
renderItem = {({ item }) => (
<View style={styles.Text2}>
<Image
source={item.image}
style={{ width: 329, height: 250, marginRight: 0 }} />
<Text style={{ marginLeft: 10, fontSize: 15, marginTop: 10 }}>
{item.content}
</Text>
<View style={{ marginLeft: 10, marginTop: 10, marginRight: 250, marginBottom: 1 }}>
<TouchableOpacity>
<Icon2
name={item.vectoricon}
size={20}
color="#b3b3b3"
/>
</TouchableOpacity>
<Text style={{ marginLeft: 25, marginTop: -20, color: "#b3b3b3" }}>{this.state.count ? '0' : '1'}</Text>
</View>
<View style={{ marginLeft: 90, marginTop: -20, marginBottom: 1, marginRight: 70 }}>
<TouchableOpacity>
<Icon1
name={item.vectoricon1}
size={20}
color="#b3b3b3"
onPress={this._incrementCountNew} />
</TouchableOpacity>
<Text style={{ marginLeft: 25, marginTop: -20, color: "#b3b3b3" }}>{this.state.count1}</Text>
</View>
<View style={styles.lineStyle} />
<View style={{ marginLeft: 10, marginTop: 1, marginRight: 270 }}>
<TouchableOpacity onPress={this._incrementCountNew}>
<Icon1
name={item.vectoricon1}
size={30}
/>
</TouchableOpacity>
</View>
<View style={{ marginLeft: 60, marginTop: -30, marginBottom: 10, marginRight: 210 }}>
<TouchableOpacity onPress={this.onlike}>
<View>
{this.state.count ? <Icon3 name={item.vectoricon2} size={30} /> : <Icon3 name={item.vectoricon3} size={30} />}
</View>
</TouchableOpacity>
</View>
</View>
)}
keyExtractor = { this.keyExtractor } />
解决方案
因为您使用的是 FlatList,如果您的列表有自己的喜欢/不喜欢和支持计数器,您是否不希望每个项目?
如果是这种情况,那么将选票数量与您所在州的单个变量联系起来是没有意义的,对吧?
你也可以像这样改进你的增量功能。
_incrementCountNew = () => {
this.setState({
count1:this.state.count1 + 1
})
}
推荐阅读
- python-3.x - 为什么不打印分配的信件
- android - 为在模拟器上运行的系统 Android 应用程序制作自定义设置向导
- node.js - 在 keycloak 中,如何仅使用状态代码进行响应而不是重定向到登录页面
- reactjs - React hooks - 如何在检查状态和道具的功能组件中实现 shoulComponentUpdate 方法?
- mysql - 根据共享的外国 ID 选择相同的项目
- java - 如何使用嵌套的 json 数组反序列化 json 对象
- c# - 无法在 Web 服务 wcf 中发送或接收大尺寸数组
- java - 我在哪里可以在 Eclipse 中找到这个小部件?
- linux - 非法选项 -p /bin/sh: 1: Usage:: not found
- java - Gigya SDK v4 Facebook 登录问题