ReactJS Program to useCallback Stable Handler with Explanation
ReactJS
Hard
Performance & Patterns
27 views
1 min read
88 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around usecallback, stable, handler. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Use useCallback to keep a stable function reference for a memoized child.
Input Format
No input.
Output Format
Render a React component.
Constraints
Wrap handler in useCallback and memoize child component.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React, { useCallback, useState } from 'react';
const Child = React.memo(function Child({ onPing }) {
return (
<button type='button' onClick={onPing} style={{ padding: '10px 14px', borderRadius: 12, border: '1px solid #ddd', background: '#fff' }}>
Ping
</button>
);
});
export default function App() {
const [pings, setPings] = useState(0);
const [count, setCount] = useState(0);
const onPing = useCallback(() => setPings((n) => n + 1), []);
return (
<div style={{ padding: 16 }}>
<h2 style={{ marginTop: 0 }}>meetcode callbacks</h2>
<Child onPing={onPing} />
<div style={{ marginTop: 10, color: '#555' }}>Pings: {pings}</div>
<button type='button' onClick={() => setCount((n) => n + 1)} style={{ marginTop: 12, padding: '8px 12px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>Unrelated state ({count})</button>
</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
Use useCallback to keep a stable function reference for a memoized child.
Input / Output
Output
Render a React component.
Constraints
Wrap handler in useCallback and memoize child component.
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, { useCallback, useState } from 'react';
const Child = React.memo(function Child({ onPing }) {
return (
<button type='button' onClick={onPing} style={{ padding: '10px 14px', borderRadius: 12, border: '1px solid #ddd', background: '#fff' }}>
Ping
</button>
);
});
export default function App() {
const [pings, setPings] = useState(0);
const [count, setCount] = useState(0);
const onPing = useCallback(() => setPings((n) => n + 1), []);
return (
<div style={{ padding: 16 }}>
<h2 style={{ marginTop: 0 }}>meetcode callbacks</h2>
<Child onPing={onPing} />
<div style={{ marginTop: 10, color: '#555' }}>Pings: {pings}</div>
<button type='button' onClick={() => setCount((n) => n + 1)} style={{ marginTop: 12, padding: '8px 12px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>Unrelated state ({count})</button>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!