Frequent Re-rendering #300
-
Is it normal for a timer to cause re-rendering of the site every second? I'm using the
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is the code thats causing the re-render: useEffect(() => {
setInterval(() => {
setRemaining(getRemainingTime())
}, 1000)
start()
}, []) Setting the state causes the component to re-render. Assuming this is for a countdown inside your prompt, you could do this instead to limit re-renders to when the prompt is active. useEffect(() => {
const interval = setInterval(() => {
if(isPrompted()) {
setRemaining(getRemainingTime())
}
}, 1000)
return () => clearInterval(interval)
}, []) You don't need to call |
Beta Was this translation helpful? Give feedback.
This is the code thats causing the re-render:
Setting the state causes the component to re-render. Assuming this is for a countdown inside your prompt, you could do this instead to limit re-renders to when the prompt is active.
You don't need to call
start
unless you setstartManually
totrue
orstartOnMount
tofalse
. Otherwise it will start automatically when the hook mounts. You should also return a callback …