how to pause useState counter value react hoooks
import React, { useState, useEffect } from 'react';
const Timer = () => {
const [seconds, setSeconds] = useState(0);
const [isActive, setIsActive] = useState(false);
function toggle() {
setIsActive(!isActive);
}
function reset() {
setSeconds(0);
setIsActive(false);
}
useEffect(() => {
let interval = null;
if (isActive) {
interval = setInterval(() => {
setSeconds(seconds => seconds + 1);
}, 1000);
} else if (!isActive && seconds !== 0) {
clearInterval(interval);
}
return () => clearInterval(interval);
}, [isActive, seconds]);
return (
<div className="app">
<div className="time">
{seconds}s
</div>
<div className="row">
<button className={`button button-primary button-primary-${isActive ? 'active' : 'inactive'}`} onClick={toggle}>
{isActive ? 'Pause' : 'Start'}
</button>
<button className="button" onClick={reset}>
Reset
</button>
</div>
</div>
);
};
export default Timer;
Also in JavaScript:
- Scroll to the top of the page using JavaScript?
- launch uikit modal from php
- FAILURE: Build failed with an exception react native android
- trim text and add ... js
- update node with nvm
- Cannot find module '@angular/cdk/a11y'
- javascript replace ios apostrophe
- add table row jQuery
- node.js discord dashboard
- asjasfawepiowjpowlklkcdlkdLkdlkskskjskknisbsbu
- set iframe height and width by js
- getstaticpaths with redux
- How to pass a map from controller to javascript function in VF page
- react Refused to execute inline script because it violates the following Content Security Policy directive
- react native text ellipsis
- javascript keep scroll visible
- javascript date format
- array map javascript mozilla
- angular one way property binding
- node check if file exists
- js replace all number
- Aspect Ratio height react
- browser detect javascript
- javascript pipe async functions