Editorial Q&A · This problem and its answer were written by the TechLab editorial team to document a common issue. It isn't a question from a community member. If you've hit a variation of it or know a better approach, add a reply below.
The problem
A common way to share user data in a React 18 + TypeScript dashboard is a single UserContext whose value holds the user's userName, theme preference and permissions. As the dashboard grows, the React DevTools profiler starts showing components re-rendering when data they don't use changes. Switching the theme, for example, re-renders a header that only displays userName.
A minimal setup that shows the behaviour:
src/contexts/UserContext.tsx:
import React, { createContext, useState, useContext, ReactNode, useMemo, useCallback } from 'react';
interface UserInfo {
userName: string;
theme: 'light' | 'dark';
permissions: string[];
}
interface UserContextType {
userInfo: UserInfo;
updateTheme: (newTheme: 'light' | 'dark') => void;
updateUserName: (newName: string) => void;
}
const UserContext = createContext<UserContextType | undefined>(undefined);
export const UserProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const [userInfo, setUserInfo] = useState<UserInfo>({
userName: 'John Doe',
theme: 'light',
permissions: ['view_dashboard', 'edit_profile'],
});
const updateTheme = useCallback((newTheme: 'light' | 'dark') => {
setUserInfo(prev => ({ ...prev, theme: newTheme }));
}, []);
const updateUserName = useCallback((newName: string) => {
setUserInfo(prev => ({ ...prev, userName: newName }));
}, []);
// The context value object itself changes on any userInfo update
const contextValue = useMemo(() => ({ userInfo, updateTheme, updateUserName }), [userInfo, updateTheme, updateUserName]);
return (
<UserContext.Provider value={contextValue}>
{children}
</UserContext.Provider>
);
};
export const useUser = () => {
const context = useContext(UserContext);
if (context === undefined) {
throw new Error('useUser must be used within a UserProvider');
}
return context;
};src/components/ProfileHeader.tsx (consumes userName only):
import React from 'react';
import { useUser } from '../contexts/UserContext';
const ProfileHeader: React.FC = () => {
const { userInfo } = useUser();
console.log('ProfileHeader re-rendered, userName:', userInfo.userName);
return (
<header style={{ padding: '10px', borderBottom: '1px solid #ccc' }}>
<h1>Welcome, {userInfo.userName}!</h1>
</header>
);
};
export default React.memo(ProfileHeader);src/components/ThemeSwitcher.tsx (consumes theme and updates it):
import React from 'react';
import { useUser } from '../contexts/UserContext';
const ThemeSwitcher: React.FC = () => {
const { userInfo, updateTheme } = useUser();
console.log('ThemeSwitcher re-rendered, theme:', userInfo.theme);
const toggleTheme = () => {
updateTheme(userInfo.theme === 'light' ? 'dark' : 'light');
};
return (
<button onClick={toggleTheme} style={{ margin: '10px' }}>
Switch to {userInfo.theme === 'light' ? 'Dark' : 'Light'} Theme
</button>
);
};
export default React.memo(ThemeSwitcher);src/App.tsx (main application component):
import React from 'react';
import { UserProvider } from './contexts/UserContext';
import ProfileHeader from './components/ProfileHeader';
import ThemeSwitcher from './components/ThemeSwitcher';
import SomeOtherComponent from './components/SomeOtherComponent'; // Imagine this is a large, complex component
const App: React.FC = () => {
return (
<UserProvider>
<ProfileHeader />
<ThemeSwitcher />
<SomeOtherComponent /> {/* This component is often far down the tree */}
<main style={{ padding: '20px' }}>
<p>Dashboard content goes here...</p>
</main>
</UserProvider>
);
};
export default App;Expected behaviour:
When the 'Switch Theme' button is clicked in ThemeSwitcher, only the ThemeSwitcher component and other components explicitly consuming userInfo.theme (or userInfo as a whole and reacting to theme changes) should re-render. ProfileHeader, which only uses userInfo.userName, should not re-render because userInfo.userName has not changed.
Actual behaviour:
Both ThemeSwitcher and ProfileHeader re-render, along with every other component that calls useUser(). The console shows:
ThemeSwitcher re-rendered, theme: light
ProfileHeader re-rendered, userName: John Doe
ThemeSwitcher re-rendered, theme: dark
ProfileHeader re-rendered, userName: John DoeIn a large dashboard where many expensive widgets read the context, this shows up as noticeable lag on slower devices.
Applies to React 18 and 19.
The question: how should the context (or the component structure) change so that components only re-render when the part of the state they use changes? Wrapping consumers in React.memo and memoizing the provider value with useMemo, as above, don't help.
