const [time, setTime] = useState(60);
// I think you are recording initially and want to stop after 60
const [recording, setRecording] = useState(true);
// you have to keep track of if user has pressed on the button or not
const [pressed,setPressed]=useState(false)
// in each second we update this and check if its current value is 0
// you could set the timer with useState but it would cause unnecessary rerenderings
let timerRef = useRef();
const countDown = () => {
setTime((prev) => prev - 1)
if (time===0){
setRecording(false)
}
};
useEffect(() => {
// every second call countDown
timerRef.current = setInterval(countDown, 1000);
// when user pressed on button, in its callback, write setPressed(true)
// or in onPressed callback you could `clearInterval(timerRef.current)`
if(pressed){
clearInterval(timerRef.current)
}
return () => clearInterval(timerRef.current);
}, [pressed]);