epam / epam/statgpt-admin-frontend
[Perf Improvement] Multiple Context Providers Causing Unnecessary Re-renders
- Dominant language
- TypeScript
- Stars
- 18
- Forks
- 0
- Avg merge
- 15h 5m
- Merged PRs (30d)
- 19
Description
- **Where:** `src/app/layout.tsx`, `src/context/*Context.tsx`
- **Why:** **Six nested Context Providers** (`NotificationProvider`, `NextAuthProvider`, `AccessControlProvider`, `SidebarProvider`, potentially others) create a deep provider tree. Any state update in a parent context re-renders all consumers, even if they don't use that state. This is especially problematic for frequently-changing contexts (e.g., `NotificationContext`).
- **Impact:** Cascading re-renders of unrelated components when sidebar toggled, notifications added, or session changes.
- **Confidence:** Medium
**Fix:**
1. **Use `useMemo` and `useCallback` in context providers** to prevent unnecessary re-renders:
```typescript
// NotificationContext.tsx
const value = useMemo(
() => ({ notifications, addNotification, removeNotification }),
[notifications]
);
return {children};
```
2. **Split contexts into separate providers** (e.g., `SidebarStateProvider` vs `SidebarDispatchProvider`) so consumers only subscribe to what they need.
3. **Consider Zustand or Jotai** as a lighter alternative to Context API for global state (if state management grows).
**Validate:**
- Use React DevTools Profiler: record interactions, check for unexpected component re-renders.
- Look for `` or custom hooks using `useContext()` that shouldn't be re-rendering.
Contributor guide
Research direction
Start by reading src/app/layout.tsx and the providers under src/context/*Context.tsx to map the nesting and current context values. Use the React DevTools Profiler to record sidebar, notification, and session interactions; done means unrelated consumers no longer show unnecessary re-renders while the existing provider behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- frontend, performance
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100