I'm new on react-native. I'm trying to make the lap button on my stopwatch to show 5 latest result. Here is my code:
laps() {
return this.state.laps.map(function(time, index) {
return <View key={index} style={styles.lap}>
<Text style={styles.lapText}>
Lap #{index 1}
</Text>
<Text style={styles.lapText}>
{formatTime(time)}
</Text>
</View>
});}
handleLapPress() {
var lap = this.state.timeElapsed;
if(this.state.running){
this.setState({
startTime: new Date(),
laps: this.state.laps.concat([lap])
});
}
else{
this.setState({
startTime: new Date(),
laps: this.setState.laps=[]
});
}}
For now, when I hit on the lap button. It show the lap time but when there are more than 5 laps. The latest one was disappear. So, what should I do to make my stopwatch show 5 latest.
CodePudding user response:
use last_five_laps as your new array to show last 5 laps
let last_five_laps = this.state.laps.slice(-5);