【发布时间】:2020-08-31 11:51:57
【问题描述】:
我正在使用两个数组 present_count 和 total_count。
present_count[0] 将存储第一个主题的计数(第一个主题单击蓝色按钮的次数),present_count[1] 将存储下一个主题的计数,依此类推。
total_count[0] 将存储第一个主题的计数(第一个主题的蓝色/红色按钮被单击的次数),total_count[1] 将存储下一个主题的数量,依此类推。
present_count[i] 将出现在斜线符号的 LHS 上,total_count[i] 将出现在斜线符号的 RHS 上。
state = {
subjects: [],
text: "",
present_count: [],
total_count: [],
}
present = i => {
this.setState({
present_count: this.state.present_count[i] + 1,
total_count: this.state.total_count[i] + 1
});
AsyncStorage.setItem('PRESENT_COUNT', this.state.present_count[i].toString());
AsyncStorage.setItem('TOTAL_COUNT', this.state.total_count[i].toString());
};
total = i => {
this.setState({
total_count: this.state.total_count[i] + 1,
});
AsyncStorage.setItem('TOTAL_COUNT', this.state.total_count[i].toString());
};
componentDidMount() {
AsyncStorage.getItem('PRESENT_COUNT').then((value) => {
this.setState({ present_count: parseInt(value) });
});
AsyncStorage.getItem('TOTAL_COUNT').then((value) => {
this.setState({ total_count: parseInt(value) });
});
}
render() {
let tick = "\u2713", cross = "\u2573";
return (
<View style={[styles.container, { paddingBottom: this.state.viewPadding }]}>
<FlatList style={styles.list}
data={this.state.subjects}
renderItem={({item, index }) => {
return (
<View>
<View style={styles.listItemCont}>
<Text style={styles.listItem}> { item.text } </Text>
<View style={styles.buttonContainer}>
<Text style={styles.listItem}>
{this.state.present_count[index]} / {this.state.total_count[index]}
</Text>
<View style={styles.button}>
<Button title={tick} onPress={() => this.present(index)} color="blue" />
</View>
<View style={styles.button}>
<Button title={cross} onPress={() => this.total(index)} color="red" />
</View>
</View>
</View>
<View style={styles.hr} />
</View>
)
}}
keyExtractor={ (item, index) => index.toString()}
/>
</View>
);
}
}
【问题讨论】:
标签: javascript arrays reactjs react-native setstate