State Not Captured Properly and Timer Causing Re-renders in Next.js App #73949
Replies: 1 comment
-
I am not sure about point 1, yet, maybe it is missing some more info. Regarding point 2, that's to be expected. Moving it into a custom hook won't do anything either, it is as good as being inlined. The, so called, standard rendering behavior is to re-render a React tree, from the component where state is set. In this case, People usually use this as workshop/demo material, showing how, one has to collocate state, where it is needed. See in that example, there's a number calculation going on, but also, it shows ticking time, and mouse position. That makes the number calculation re-run every second, and every time the mouse moves. The solution in that case, is to notice that these are independent pieces of state, and collocate them with the UI that uses them, further down in the React tree. For your case, though, you want to know the time since the user starts, so why not just store the |
Beta Was this translation helpful? Give feedback.
-
Summary
So basically I am facing two issues with this piece of code.
Finish Button Behavior: When the user clicks on the "Finish" button (handleFinish) for the first time, they are redirected to the start of the same page instead of the summary page. However, in the case of a retake, clicking on the "Finish" button correctly displays the summary page. It seems the issue is related to the state not being properly captured or updated during the initial attempt, but I'm unable to pinpoint the exact problem.
Constant Re-renders: The page is undergoing constant re-renders, which is evident from the console logs, even though there is no user interaction on the page. I suspect this is related to the running timer. I tried moving the timer logic to a custom hook to prevent re-renders, but that didn’t resolve the issue. While this behavior isn’t visible to a normal user, it is noticeable in the console logs.
I am using NextJs 14.2. Could you please help me with this issue?
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions