Memoized Presentational Component

Memoized Presentational Component

Hard ReactJS Performance & Patterns 20 views
Explanation Complexity

Problem Statement

Create a presentational component and wrap it with React.memo.

Input Format

No input.

Output Format

Render a React component.

Constraints

Avoid re-rendering when props do not change.

Input / Output Format

Input Format
No input.
Output Format
Render a React component.
Constraints
Avoid re-rendering when props do not change.

Examples

Input:
Output:
(React code)

Example Solution (Public)

ReactJS
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>
  );
}

Official Solution Code

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>
  );
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.