I am trying to add a lapTime to a totalLaptime state using Reactjs.
I have started by putting time in to milliseconds i.e 81300 which equates to 01:21:300 (1 minute 21 seconds 300 milliseconds). I then have a button which onClick adds a new lapTime and then i add lapTime to totalLapTime. Lastly, i then have a function that takes the total milliseconds and converts it to a readable format i.e 01:21:300.
I have my state:
this.state = {
lap: 0,
lapTime: 0,
totalLapTimes: 0,
totalLapTimeCalc: ''
};
My click function to move on to the next lap
handleClick = () => {
this.setState(({lap, tyres, deg}) => ({
lap: lap + 1,
lapTime: this.randomLap(), <-- function makes a random milliseconds
totalLapTimes: + this.state.lapTime + this.state.totalLapTimes,
totalLapTimeCalc: this.lapTimeCalc(this.state.totalLapTimes)
}));
};
The function to convert the time from milliseconds to readable format:
lapTimeCalc = (ms) => {
return new Date(ms).toISOString().slice(11, -1);
};
Expected result should be 01:21:xxx added to totalLapTimeCalc after each click on the lap button.
At the moment, when i click the lap button i have to click it 3 times before the totalLapTime is even converted, and by then the totalLapTimeCalc is incorrect.