【发布时间】:2019-01-02 03:36:55
【问题描述】:
我有一个 <TouchableWithoutFeedback> 包裹在一个封闭视图周围,我想在单击它时更改这个封闭视图的颜色,即 onPressIn ,但是所有视图的颜色都会改变,我的意思是映射视图,请问我怎样才能让 onPressIn 只改变那个特定视图的样式而不是全部的样式
const herbs = this.state.record.map(herb => (
<TTouchableWithoutFeedback
onPressIn={() => this.setState({ pressed: !this.state.pressed })}
key={herb.id}
>
<View style={this.state.pressed ? BackStyles.herbBox : BackStyles.herb_box}>
<Image
style={BackStyles.image}
source={{ uri: `${herb.name.replace(/ /g, "")}` }}
/>
<View style={{ flexDirection: "column" }}>
<Text style={BackStyles.header}>{herb.name}</Text>
<Text style={BackStyles.sub}>{herb.bot}</Text>
</View>
</View>
</TTouchableWithoutFeedback>
));
const BackStyles = StyleSheet.create({
herb_box: {
backgroundColor: "#fff",
borderRadius: 7,
marginTop: 10,
marginBottom: 10,
flexDirection: "row",
width: "95%",
alignSelf: "center"
// height: '2%'
// justifyContent: 'center',
},
herbBox: {
backgroundColor: "#28B564",
borderRadius: 7,
marginTop: 10,
marginBottom: 10,
flexDirection: "row",
width: "95%",
alignSelf: "center"
}
});
【问题讨论】:
-
当你做
this.setState时,它会重新渲染所有 -
知道如何避免使用 this.setstate 吗?
-
你最好使用
Flatlist来达到这个目的
标签: javascript css reactjs react-native