score:2
Accepted answer
you wont need setinterval
. let useeffect
run only when the timer changes and this is done by passing the timer
as argument.
this link describe how to optimize useeffect
const [timer, settimer] = usestate(10000);
function settimertonewvalue() {
settimer(timer - 100);
}
useeffect(() => {
if (timer > 6000) {
settimertonewvalue();
}
}, [timer]);
console.log(timer);
in this example useeffect
will execute as soon as timer
changes.
here is another implementation with setinterval
function settimertonewvalue() {
settimer((timer) => timer - 100);
}
useeffect(() => {
let z;
if (timer > 6000) {
z = setinterval(() => {
settimertonewvalue();
}, 1000);
}
return () => clearinterval(z);
}, [timer]);
score:1
if you have to use timer you can use this code
export default function timer() {
const [timer, settimer] = usestate(10000);
useeffect(() => {
console.log(timer)
const interval = setinterval(() => {
if (timer>6000)
settimer(timer => timer - 1000);
}, 1000);
return () => clearinterval(interval);
})
}
Source: stackoverflow.com
Related Query
- How to check if a number is greater than another number inside useEffect
- how to validate if one number is greater than other inside form rule?
- How to check if any one of the variable is greater than 0
- how do you prevent functions inside useEffect from being triggered more than once?
- Showing the count (in render) only if it is greater than 0 after the data is loaded inside useEffect
- How to return a false or true inside the useEffect and pass it to another useEffect method
- How to trigger an useEffect if the array is greater than
- How to access state from another function inside React useEffect
- How to loop over a number in React inside JSX
- How to test useEffect with async function and setState inside
- Jestjs how to test function being called inside another function
- How do I check if the user clicked inside the current component?
- Redux - how to call an action creator from inside another action creator?
- You cannot render a <Router> inside another <Router>. You should never have more than one in your app
- How to dispatch an redux action to load data inside useEffect on page load
- React - How do you call a function from inside another function
- How to add all unknown words by Code Spell Checker to dictionary or ignore them when language is another than english?
- How to check if state default boolean false is changing to true with React Hook useEffect
- How to cancel all subscriptions inside UseEffect in React
- reactjs how to filter / check if a any object in an array is present in another array of objects?
- How to fetch data asynchronously inside component and pass it as props to another component in render?
- Any idea how to check if a div contains ONLY an image tag inside it?
- How to access from state another key inside the state in React
- How do we check the boolean value in useEffect in react hooks?
- How can I convert a string to a number inside of a useState Hook?
- How can I make a request to an API based on the response from another request? React Hook "useSwr" cannot be called inside a callback
- How to use useEffect and the Context API to check if a user is logged in and protect a route?
- How do I correctly add data from multiple endpoints called inside useEffect to the state object using React Hooks and Context API?
- How to target a ref inside a component from another component?
- Need to check if the value being submitted is greater than the current value
More Query from same tag
- Why is react returning a TypeError: Cannot read properly 'join' of undefined?
- Can anyone please tell me what's wrong this socket event?
- Setting dynamic state data in Chart.JS + React
- React Hooks UseRef issue
- Three react fiber OrbitControls ref object throws error in typescript at build phase (code does work)
- onAuthStateChanged Firebase Listener on app refresh causing private route issues
- How to prevent component from rendering child until data fetches
- Redux. Pass state value to reducer
- Calling a function a nested state object in ReactJS
- How to map the objects that having the dynamic keys in React?
- Open a default input in form tag
- ReactJS - Unexpected "`"
- axios.interceptors do not fire when fetching with php proxy
- React - Axios call make too many requests
- Place styles generated by material-ui for children components after style generated for parent component?
- How to get item from local storage?
- why is this.state.records.amount undefined?
- React Rerender component not working, Cannot read property 'forceUpdate' of undefined
- React on different subdomains
- How to resolve TypeError: this.state.person is undefined
- React with Ant Design - How to share state between multiple <Select> on a <Form>?
- process is not defined on hot reload
- ReferenceError: login is not defined at submitHandler
- Using axios or async network call in the actions of the redux, updating the initial state
- Getting 400 Error while Authenticating user credentials using fetch API
- How to mock eventListener when set in componentDidMount()
- How to Pass A variable in json to get data
- Material UI's CSSBaseline breaking react-mentions
- Load GLTF file that is not located in Public folder
- Transferring form input value to var