【发布时间】:2018-09-08 22:03:21
【问题描述】:
我是 React Native 的新手,到目前为止我的大部分进步都归功于我疯狂的复制粘贴技能。
目标:我希望它只为单个订单项切换删除线 onPress。
据我所知,当用户按下 TouchableOpacity > ListItem 时:
- 它应该切换
strikeStatus, - 将添加
styles.strike - 基于
titleStyle中的this.state.strikeStatus ? styles.strike : false测试。
具有讽刺意味的是,当它最初加载时,我可以让它为所有 ListItems 工作,但我不能让它切换。请指教。
(它说在 TouchableOpacity 和 ListItem 中都放置一个onPress,但这对我来说毫无意义。如果可行,请提供更详细的说明。)
Expo Snack 是here。
`export default class App extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
items: [],
strikeStatus: false,
};
}
toggleStrike = (item) => {
this.setState({
strikeStatus: true
});
};
render() {
return (
<Header
placement="left"
leftComponent={{ icon: 'menu', color: '#fff' }}
centerComponent={{
text: 'Shopping List',
style: { fontFamily: 'Verdana', fontSize: 18, color: '#fff' },
}}
rightComponent={{ icon: 'settings', color: '#fff' }}
/>,
<List containerStyle={styles.container}>
<FlatList
data={this.state.items}
showsVerticalScrollIndicator={true}
renderItem={({ item }) => (
<TouchableOpacity
onPress={() => this.toggleStrike(this)}>
<ListItem
style={styles.item}
titleStyle={[
styles.item,
this.state.strikeStatus ? styles.strike : false
]}
title={item.Name}
subtitle={item.Updated}
badge={{
value: item.Buy,
textStyle: styles.badge
}}
/>
</TouchableOpacity>
)}
/>
</List>
);
}
}
const styles = StyleSheet.create({
strike: {
textDecorationLine: 'line-through',
textDecorationStyle: 'solid'
}
});`
【问题讨论】:
标签: react-native react-native-flatlist