Split Big Component

Split Big Component

Medium ReactJS Component Design 28 views
Explanation Complexity

Problem Statement

Refactor by splitting a ProfileCard into Avatar and Details components.

Input Format

No input.

Output Format

Render a React component.

Constraints

Use small components and props.

Input / Output Format

Input Format
No input.
Output Format
Render a React component.
Constraints
Use small components and props.

Examples

Input:
Output:
(React code)

Example Solution (Public)

ReactJS
import React from 'react';

function Avatar({ initials }) {
  return (
    <div style={{ width: 56, height: 56, borderRadius: '50%', background: '#0b5', color: '#fff', display: 'grid', placeItems: 'center', fontWeight: 700 }}>
      {initials}
    </div>
  );
}

function Details({ name, subtitle }) {
  return (
    <div>
      <strong>{name}</strong>
      <div style={{ color: '#555' }}>{subtitle}</div>
    </div>
  );
}

function ProfileCard({ name }) {
  const initials = name.split(' ').filter(Boolean).map((p) => p[0].toUpperCase()).slice(0, 2).join('');
  return (
    <div style={{ display: 'flex', gap: 12, alignItems: 'center', border: '1px solid #eee', borderRadius: 14, padding: 14, width: 420 }}>
      <Avatar initials={initials} />
      <Details name={name} subtitle='Learning React on meetcode' />
    </div>
  );
}

export default function App() {
  return <ProfileCard name='Meetcode Learner' />;
}

Official Solution Code

import React from 'react';

function Avatar({ initials }) {
  return (
    <div style={{ width: 56, height: 56, borderRadius: '50%', background: '#0b5', color: '#fff', display: 'grid', placeItems: 'center', fontWeight: 700 }}>
      {initials}
    </div>
  );
}

function Details({ name, subtitle }) {
  return (
    <div>
      <strong>{name}</strong>
      <div style={{ color: '#555' }}>{subtitle}</div>
    </div>
  );
}

function ProfileCard({ name }) {
  const initials = name.split(' ').filter(Boolean).map((p) => p[0].toUpperCase()).slice(0, 2).join('');
  return (
    <div style={{ display: 'flex', gap: 12, alignItems: 'center', border: '1px solid #eee', borderRadius: 14, padding: 14, width: 420 }}>
      <Avatar initials={initials} />
      <Details name={name} subtitle='Learning React on meetcode' />
    </div>
  );
}

export default function App() {
  return <ProfileCard name='Meetcode Learner' />;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.