Skip to content
Advertisement

How to switch input radio button checked every 3 seconds?

I am trying to switch between input radio button checked every 3 second in Next.js, and it is switching from case0 to case1 and case1 to case2 but not case2 to case0. I have intermediate level of knowledge in React and Next.js.

JavaScript

Advertisement

Answer

As @Phil mentioned in his answer, you should wrap the setInterval logic inside a useEffect to avoid creating a new setInterval every time the component re-renders. I would also suggest you move the count variable to state and use that directly to update the inputs checked attributes, which avoids creating 3 separate states for it.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement