Skip to the solution
EasyPhone Screen6 min read

What are React Components?

138 views

The problem

Question presented to candidate: "What is a component in React, and what makes something a good one?"

What a strong answer should cover:

  • A component is a reusable, self-contained piece of UI: a function that takes props and returns a description of what to render.
  • Naming rule: must start with a capital letter, because lowercase JSX names compile to host-element strings.
  • Function components are the modern default; class components are legacy but still supported and still required for Error Boundaries.
  • Function components have no instance β€” there is no this; state lives in Hooks keyed by call order.
  • Props are read-only. A component must never mutate them.
  • Purity: given the same props and state, a component should return the same output and cause no side effects during render.
  • Reuse is by composition, not inheritance β€” React has no component inheritance story at all.
  • Components can return arrays, strings, numbers, null, and Fragments β€” not just a single element.

Clarifying questions expected:

  • "Function or class β€” is this a legacy codebase?"
  • "Do you want the definition, or how I decide where to draw component boundaries?"

Code / implementation expected: Yes β€” a small component taking props, plus one composing another.

  • components
  • reusability
  • ui
  • functional components

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Try it in the playground

Open any example in the playground to run and change it.

ReactProps in, callbacks up, and composition via children
import { useState } from "react";

// A presentational component: props in, UI out, no state of its own.
// It knows nothing about where it is used β€” that is what makes it reusable.
function Avatar({ name, size = 40 }) {
  const initials = name.split(" ").map((w) => w[0]).join("").toUpperCase();
  return (
    <div style={{
      width: size, height: size, borderRadius: "50%", background: "#4f46e5",
      color: "white", display: "grid", placeItems: "center", fontSize: size / 2.5,
    }}>
      {initials}
    </div>
  );
}

// A component taking a CALLBACK β€” the only way data travels back up.
// Note it never mutates props; it calls what the parent handed it.
function UserRow({ user, onSelect }) {
  return (
    <li style={{ display: "flex", alignItems: "center", gap: 10, padding: "6px 0" }}>
      <Avatar name={user.name} size={32} />
      <span style={{ flex: 1 }}>{user.name}</span>
      <button onClick={() => onSelect(user.id)}>select</button>
    </li>
  );
}

// A component composing arbitrary content through the children prop β€” the
// generic slot that makes wrappers reusable without knowing what goes inside.
function Card({ title, children }) {
  return (
    <section style={{ border: "1px solid #ddd", borderRadius: 10, padding: 16, marginBottom: 14 }}>
      <h4 style={{ margin: "0 0 10px" }}>{title}</h4>
      {children}
    </section>
  );
}

const USERS = [
  { id: 1, name: "Ada Lovelace" },
  { id: 2, name: "Grace Hopper" },
  { id: 3, name: "Alan Turing" },
];

export default function App() {
  const [selected, setSelected] = useState(null);
  const chosen = USERS.find((u) => u.id === selected);

  return (
    <div style={{ padding: 24, fontFamily: "system-ui", maxWidth: 420 }}>
      <Card title="Team">
        <ul style={{ listStyle: "none", padding: 0, margin: 0 }}>
          {USERS.map((u) => (
            <UserRow key={u.id} user={u} onSelect={setSelected} />
          ))}
        </ul>
      </Card>

      {/* The same Card wrapper, completely different children */}
      <Card title="Selection">
        {chosen ? (
          <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
            <Avatar name={chosen.name} size={48} />
            <strong>{chosen.name}</strong>
          </div>
        ) : (
          <em style={{ color: "#666" }}>Nothing selected yet.</em>
        )}
      </Card>
    </div>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 23 of 119 in React.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All React.js questions
Up nextEasy

What is the role of a bundler like Webpack in a React project?

Continue