Skip to content

Bug: Persistent Re-render Issue Despite Optimizations (React 18, Next.js, Hooks, Context) #33034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
azorlu80 opened this issue Apr 26, 2025 · 2 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@azorlu80
Copy link

azorlu80 commented Apr 26, 2025

Bug Report: Excessive Re-renders in Component Despite Multiple Optimizations

Description

A specific component in our Next.js application exhibits a high number of re-renders (approx. 8-10) even after all asynchronous data fetching (from APIs and Context) is complete and related loading states are false. This occurs despite applying various standard React optimization techniques, including memoizing context providers, functions, and derived state.

Steps to Reproduce

Unfortunately, isolating this issue into a minimal reproducible example has proven difficult due to the component's reliance on multiple contexts and asynchronous operations within our specific application structure. However, the core setup involves:

  1. A dynamic route page component (e.g., /app/items/[itemId]/details/[detailId]).
  2. The component uses useEffect to fetch data from two separate API endpoints based on route parameters (itemId, detailId) upon mount and when parameters change.
  3. The component consumes data from two separate contexts (AuthContext, FeatureFlagContext) using useContext.
  4. The component manages multiple states using useState for the fetched data, loading status, and error status for both API calls.
  5. Data fetching functions are wrapped in useCallback.
  6. Context providers for AuthContext and FeatureFlagContext have their value prop memoized using useMemo.
  7. Derived state values (e.g., isPageLoading) are memoized using useMemo.
  8. All major functions within the component are memoized using useCallback.
  9. An attempt was made to consolidate multiple loading/error states into a single state object, which did not resolve the issue and potentially increased renders.
  10. React Strict Mode is confirmed to be disabled.

Expected Behavior

After the initial loading phase and subsequent state updates settle (i.e., all loading states are false, data is populated), the component should render only once or twice more at most, ideally stabilizing without further renders until user interaction or prop changes occur.

Actual Behavior

Console logs show the component rendering approximately 8-10 times after all loading states (bolumLoading, previewsLoading, limitLoading) are false. This suggests that the sequence or batching of state updates within the asynchronous data fetching functions (fetchItemData, fetchDetailData) might be triggering an unexpected cascade of renders, even though the final state values are stable. The number of renders sometimes appeared to increase after applying useCallback to more functions or attempting state collocation.

Environment

  • React: ^18
  • Next.js: ^15.3.1
  • Node.js: [22.14.0]
  • OS: Linux (Pop!_OS 22.04 LTS)
  • Browser: [1.77.97 Chromium: 135.0.7049.84 ]

Additional Context

We have applied standard optimization techniques (useMemo for contexts/derived state, useCallback for functions) as documented. The persistence of these extra renders after stabilization is unexpected and difficult to debug further without deeper insight into React's internal scheduling or potential interactions between multiple asynchronous state updates within useEffect.

@azorlu80 azorlu80 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Apr 26, 2025
@alapollon
Copy link

Hi there!, I've read your issue here. And however challenging I am wondering. if you would be interested in performing. A mock and spies test of your web bundle. To upload or email the results anywhere for diagnostics. It will be useful to help sort out the dependency

This was referenced Apr 27, 2025
@cgx-system
Copy link

A useEffect with only one dependency and a simple console.log can help you check if any prop or state changes multiple times.
If not a prop or state, then you can do the same in the parent component, and so on until you find which one in the tree is re-rendering.
Can you share your code to help understand where the issue comes from ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

3 participants