← Back to React, page 6

react-0109 · Easy fresher

What is the difference between a React component, a React element and a component instance?

A component is a function (or class) that describes a piece of UI. An element is the plain object that describes one use of a component or a DOM tag, produced by JSX, such as <Counter />. An instance is the running copy that React creates and keeps in its tree for an element, holding its own state. Two elements of the same component make two instances with separate state.

Full answer

Three words are often mixed up, and interviewers use them to check whether you understand how React works.

Component. A reusable definition: function Counter() { ... }. It is only a blueprint. Writing it runs nothing.

Element. A lightweight, immutable description of what you want on screen: <Counter label="A" /> creates an object like { type: Counter, props: { label: 'A' } }. Elements are cheap and thrown away on each render. An element can describe a DOM node (type: 'button') or a component (type: Counter). React never changes an element. When something changes, you return a new one.

Instance. When React renders an element for the first time, it creates the thing that lives in its internal tree for that position (a Fiber node with state, refs and effects). For a class component, that is a real object created with new. For a function component, there is no visible object, but React stores hooks state for it in the same way, so it behaves like an instance. State belongs to the instance, not to the component definition. That is why you can render the same component twice and get independent state.

What keeps an instance alive: its type and its position in the tree (and key). If the same component type is rendered at the same position in the next render, React keeps the instance and updates props. If the type or key changes, React unmounts it, which destroys its state, and creates a new one.

Why this matters: it explains why state resets when a component moves, why keys matter, and why you cannot call a component as a normal function and keep its state.

The example renders two counters from one component and checks that their state is independent.

jsx
import { useState } from 'react';

function Counter({ label }) {
  const [count, setCount] = useState(0);
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Add to {label}</button>
      <p>
        {label}: {count}
      </p>
    </div>
  );
}

const element = <Counter label="X" />;
console.log('component is a function: ' + (typeof Counter === 'function'));
console.log('element is an object with type === Counter: ' + (typeof element === 'object' && element.type === Counter));

export default function App() {
  return (
    <div>
      <Counter label="First" />
      <Counter label="Second" />
    </div>
  );
}

Counter is the component, each tag in App creates an element, and React keeps two instances. The check pressed Add to First twice and left the second counter alone. Output of the run: component is a function: true element is an object with type === Counter: true after two clicks on the first counter only: First: 2 | Second: 0

Key points

  • A component is a definition (function or class). An element is a plain object describing a use of it.
  • An instance is what React keeps in its tree, with its own state, for an element.
  • Rendering the same component twice makes two instances with separate state.
  • React keeps an instance while type, position and key stay the same.
  • Elements are cheap and immutable. Instances hold state.

Common mistakes

  • Calling a component function directly instead of rendering an element.
  • Thinking state is shared between two uses of the same component.
  • Saying a component is the same as an element.
  • Changing an element after creating it.
  • Forgetting that changing a component's type at a position resets its state.

Follow-ups

  • What happens to state when the same position renders a different component type?
  • How do keys affect instances?
  • Why can function components be called "instances" even though there is no object?

Tips

  • Use the blueprint, description and live copy wording.
  • Mention type and position as what keeps the instance alive.
  • Tie it to state resets and keys.