ReactJS Program to Memoize Context Value with Explanation
ReactJS
Hard
Performance & Patterns
16 views
1 min read
83 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around context, react, memoize. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Avoid re-rendering consumers by memoizing the context value object.
Input Format
No input.
Output Format
Render a React component.
Constraints
Keep provider value stable with useMemo.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React, { createContext, memo, useContext, useMemo, useState } from 'react';
const Ctx = createContext(null);
const Panel = memo(function Panel() {
const ctx = useContext(Ctx);
return (
<div style={{ border: '1px solid #eee', borderRadius: 14, padding: 12 }}>
<strong>meetcode panel</strong>
<div style={{ marginTop: 6, color: '#555' }}>Theme: {ctx.theme}</div>
<button type='button' onClick={ctx.toggle} style={{ marginTop: 10, padding: '8px 12px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>Toggle</button>
</div>
);
});
export default function App() {
const [theme, setTheme] = useState('light');
const value = useMemo(() => {
return {
theme,
toggle: () => setTheme((t) => (t === 'light' ? 'dark' : 'light'))
};
}, [theme]);
return (
<div style={{ padding: 16, width: 520 }}>
<Ctx.Provider value={value}>
<Panel />
</Ctx.Provider>
</div>
);
}
Output Example
No sample I/O is provided for this question.
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Avoid re-rendering consumers by memoizing the context value object.
Input / Output
Output
Render a React component.
Constraints
Keep provider value stable with useMemo.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
import React, { createContext, memo, useContext, useMemo, useState } from 'react';
const Ctx = createContext(null);
const Panel = memo(function Panel() {
const ctx = useContext(Ctx);
return (
<div style={{ border: '1px solid #eee', borderRadius: 14, padding: 12 }}>
<strong>meetcode panel</strong>
<div style={{ marginTop: 6, color: '#555' }}>Theme: {ctx.theme}</div>
<button type='button' onClick={ctx.toggle} style={{ marginTop: 10, padding: '8px 12px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>Toggle</button>
</div>
);
});
export default function App() {
const [theme, setTheme] = useState('light');
const value = useMemo(() => {
return {
theme,
toggle: () => setTheme((t) => (t === 'light' ? 'dark' : 'light'))
};
}, [theme]);
return (
<div style={{ padding: 16, width: 520 }}>
<Ctx.Provider value={value}>
<Panel />
</Ctx.Provider>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!