This is my first project in React (find some tutorial on youtube) and i am a little confused, i am trying to make simple pomodoro timer. Right now count starts when page opens. What i want is to add a little more functionality, like pause/stop the timer buttons, and of course start it when button is pressed. also I would like to add some more things in the future, but at the moment I just want to understand how to make button work
This is how it looks and working fine (without buttons).
import React, { useEffect, useState } from "react";
function App() {
const [minutes, setMinutes] = React.useState(25);
const [seconds, setSeconds] = React.useState(0);
const [displayMessage, setDisplayMessage] = useState(false);
useEffect(() => {
let interval = setInterval(() => {
clearInterval(interval);
if (seconds === 0) {
if (minutes !== 0) {
setSeconds(59);
setMinutes(minutes - 1);
} else {
let minutes = displayMessage ? 24 : 4;
let seconds = 59;
setSeconds(seconds);
setMinutes(minutes);
setDisplayMessage(!displayMessage);
}
} else {
setSeconds(seconds - 1);
}
}, 1000);
return () => {};
}, [seconds]);
const timerMinutes = minutes < 10 ? `0${minutes}` : minutes;
const timerSeconds = seconds < 10 ? `0${seconds}` : seconds;
return (
<div className="timer">
<h2>
{timerMinutes}:{timerSeconds}
</h2>
<button className="buttons_new">Start</button>
<button className="buttons_new">Pause</button>
<button className="buttons_new">Stop</button>
</div>
);
}
export default App;
I tried to use some advices from same situations (like using useCallback instead of useEffect, just remove useEffect and make it as normal function) but when i try and add this to button it works only 1 time instead of refreshing every second. So if i click on button time will change from 25:00 to 24:59 and that's all. How can i make it work and dynamically update after clicking the start button? my terrible and stupid attempts below:
const startTimer = () => {
//same code as above
<button className="buttons_new" onClick={startTimer}>Start</button>
Also trying to do something like this(effect was the same):
const startTimer = useCallback(() => {