score:3
Accepted answer
it's best to run this kind of effect in a useeffect
hook. that way, you can run the effect whenever counter
is updated and provide a mechanism to cancel an existing timeout.
when the timeout
hits 1 second, the counter
stateful variable will be incremented. since counter
is specified in the useeffect
dependency array, the effect will run again, queuing up another timeout.
we return a cleanup function from our usereffect
hook. this is important because, if our counter
is changed by some other mechanism (e.g., the reset
button), we'll want to cancel the in-progress timeout to start over!
import react, { usestate, useeffect } from "react";
const app = (props) => {
const [counter, setcounter] = usestate(0)
useeffect(() => {
const timeout = settimeout(
() => setcounter(counter + 1),
1000
)
return () => {
cleartimeout(timeout);
}
}, [counter])
return (
<div>
<h1>{counter}</h1>
<button onclick={()=>setcounter(0)}>reset</button>
</div>
)
}
Source: stackoverflow.com
Related Query
- What happens if state is updated multiple times?
- What happens when using this.setState multiple times in React component?
- React component table is being rendered multiple times or duplicated after updating state
- Prevent re-rendering when state is changed multiple times during useEffect
- UseEffect hook executes multiple times on updating state
- Why does state not update correctly when calling setState() multiple times
- Running setInterval() inside React function happens multiple times and increases the amount rapidly over time
- Updating Parent component state from multiple child components not taking updated state value into account
- What is the difference between using setItem to set multiple state values and using useEffect to do so?
- What is the concept of specifiying same declarations multiple times in interface and classes?
- React state updating multiple times on single setState call
- How to update state multiple times on same event using React useState hook?
- Updating state multiple times within a setTimeout
- Setting state on array logs empty array multiple times
- What happens if I give same state to a react component
- React state getting set multiple times with blank value
- What happens if pass function component as state variable?
- setState getting updated multiple times
- How to render a React component multiple times but each time with an updated State?
- Next.js, loading a library client side, the component is re rendered twice and multiple times as the parent component state changes
- What is a better approach to searching a large array for a particular entry multiple times on the same page?
- useEffect Setting Redux State multiple Times
- Changing the state of one component that gets declared multiple times in React - Better Practices?
- multiple set gets updated while updating one state in react
- React setState called multiple times on the same state object
- useEffect being triggered multiple times and I do not know what to change
- How to work around updating state multiple times in a for loop? ReactJS
- How to update a state array multiple times faster than the setState without overwriting?
- I just try to check what happens componentdidmount change state and value after render calls but get unexpected result
- Unable to get updated state after call multiple dispatch
More Query from same tag
- Inserting axios result using hooks
- getting different output when calling function draw directly? React Tetris
- Can I use DRY Concept for this code in React JS?
- HTML elements not rendering on Gatsby website
- How to render a Link in useState - nextjs
- Selection Checkbox in React using Hooks
- e.target.value in button returns undefined sometimes. Why this happens?
- How to intercept browser navigation and prompt user in React (including Browser Navigation Away and Tab close)?
- Ant Design SSR mismatch issue, Server renders Chinese but client EN
- Call a function in a sibling component
- Using a read-once variable loaded async through react-hook
- How to change the value in the array according to the index in react hooks
- @tinymce/tinymce-react - How do I change in code the focus?
- Deck.gl globe advice
- How to attach a payload with GET request using axios?
- React alert Uncaught Invariant Violation
- Cards with different length descriptions are misaligned
- Redux form with self creating inputs
- jsPDF addSvgAsImage, canvg is not defined
- Calling a component by name in React Router
- this.props.history.push("/") isn't redirecting me to homepage
- How to select every two / three children or sibling elements and apply styling?
- React Router multiple params doesn't work
- How to send an email using javascript and not change the window location?
- How to change color of fontawesome icons in modular css React
- Initializing useContext Types for Custom Hook
- Class constructor type extending another and intersecting at the same time
- How to use this Javascript animation in React.js
- How to scroll to bottom after an element appears (using react-slidedown)
- Passing data into a React component within existing app