Performance: Creating context
This affects interaction responsiveness and rendering speed by controlling how components share data without prop drilling.
const CountContext = React.createContext(); function App() { const [count, setCount] = React.useState(0); const value = React.useMemo(() => ({ count, setCount }), [count, setCount]); return ( <CountContext.Provider value={value}> <ComponentA /> <ComponentB /> <ComponentC /> </CountContext.Provider> ); } // Components only re-render when count changes, memoized value prevents unnecessary renders
const MyContext = React.createContext(); function App() { const [count, setCount] = React.useState(0); return ( <MyContext.Provider value={{ count, setCount }}> <ComponentA /> <ComponentB /> <ComponentC /> </MyContext.Provider> ); } // ComponentA, B, C all consume context and re-render on count change
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Context value recreated every render | Many components re-render | Multiple reflows if DOM changes | High paint cost due to many updates | [X] Bad |
| Memoized context value with useMemo | Only components using changed data re-render | Minimal reflows | Lower paint cost | [OK] Good |
createContext() function?createContext() doescreateContext().createContext() from the React object: React.createContext().useContext(), which is for consuming context, not creating it.const MyContext = React.createContext('default');
function Display() {
const value = React.useContext(MyContext);
console.log(value);
return null;
}
function App() {
return (
<MyContext.Provider value="hello">
<Display />
</MyContext.Provider>
);
}Display component is inside a provider with value "hello".Display is inside the provider, useContext(MyContext) returns "hello".const ThemeContext = React.createContext('light');
function Button() {
const theme = React.useContext();
return <button>Theme is {theme}</button>;
}useContext hook requires the context object as an argument. Here, it is called without any argument.createContext. Provider is optional but not an error here. Functional components can use hooks, so no need for class-based.createContext('light').useContext(ThemeContext) correctly. const ThemeContext = React.createContext('light');
function ThemeProvider({ children }) {
const [theme, setTheme] = React.useState('dark');
return (
<ThemeContext.Provider value={theme}>
{children}
</ThemeContext.Provider>
);
}
function DisplayTheme() {
const theme = React.useContext(ThemeContext);
return <div>Current theme: {theme}</div>;
} attempts to use state but misses the React. prefix on useState, causing ReferenceError. const ThemeContext = React.useContext('light');
function ThemeProvider({ children }) {
const theme = 'dark';
return (
<ThemeContext.Provider value={theme}>
{children}
</ThemeContext.Provider>
);
}
function DisplayTheme() {
const theme = React.useContext(ThemeContext);
return <div>Current theme: {theme}</div>;
} incorrectly calls React.useContext('light') instead of createContext(). const ThemeContext = React.createContext();
function ThemeProvider({ children }) {
const theme = 'dark';
return (
<ThemeContext.Provider value={theme}>
{children}
</ThemeContext.Provider>
);
}
function DisplayTheme() {
const theme = React.useContext();
return <div>Current theme: {theme}</div>;
} calls useContext() without argument, causing error.Pass the test and this pattern moves unknown