Skip to content

Frequent Re-rendering #300

Discussion options

You must be logged in to vote

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 start unless you set startManually to true or startOnMount to false. Otherwise it will start automatically when the hook mounts. You should also return a callback …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@qapla47
Comment options

Answer selected by qapla47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants