Memoized Presentational Component
ReactJS
Hard
5 views
Problem Description
Create a presentational component and wrap it with React.memo.
Output Format
Render a React component.
Constraints
Avoid re-rendering when props do not change.
Official Solution
import React, { useState } from 'react';
const TopicChip = React.memo(function TopicChip({ name }) {
return <span style={{ padding: '8px 10px', border: '1px solid #ddd', borderRadius: 999 }}>{name}</span>;
});
export default function App() {
const [count, setCount] = useState(0);
return (
<div style={{ padding: 16 }}>
<div style={{ display: 'flex', gap: 10 }}>
<TopicChip name='HTML' />
<TopicChip name='CSS' />
<TopicChip name='React' />
</div>
<button type='button' onClick={() => setCount((c) => c + 1)} style={{ marginTop: 12, padding: '10px 14px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>
Clicked {count}
</button>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!