Render props and higher-order components

Intermediate · 7 min

The pre-hooks patterns for sharing stateful logic, worked examples of each, and why hooks replaced most of their everyday uses.

Before thisCompound componentsThe rules of hooks

Developer Way · 7 min

Render props and higher-order components

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Developer WayReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A render prop is a component that takes a function as a prop and calls it with its internal state, letting the caller decide what to render with that state.
  • A higher-order component (HOC) is a function that takes a component and returns a new component wrapping it with extra behavior or props.
  • Both patterns exist to share stateful logic between components that are not related by composition, which was hard to do before hooks.
  • A custom hook does the same job with less ceremony: no extra wrapper component, no extra level of nesting in the rendered tree, and no awkward prop-name collisions.
  • Most new React code reaches for a custom hook instead, but render props and HOCs did not disappear — they show up in library APIs, like React Router's old render-prop routes or libraries that export a `withX` wrapper.
  • Understanding both patterns still matters for reading older codebases and some current library documentation, even if you rarely write a new one from scratch.

The problem: sharing stateful logic before hooks existed

Before hooks, a component’s state and lifecycle logic lived inside a class, and there was no way to “extract” a piece of that logic into a standalone, reusable unit the way a function can be extracted today. Two patterns grew up to work around that limit: render props and higher-order components. Both are still valid React, and both are worth recognizing even though a custom hook is almost always the better choice for new code.

Render props: handing control of the output to the caller

A render-prop component manages some piece of state internally, then calls a function prop with that state instead of rendering fixed markup itself:

function MouseTracker({ children }) {
  const [position, setPosition] = useState({ x: 0, y: 0 });

  function handleMouseMove(event) {
    setPosition({ x: event.clientX, y: event.clientY });
  }

  return <div onMouseMove={handleMouseMove}>{children(position)}</div>;
}

A caller decides what to render with that position, without needing to know how MouseTracker tracks it:

<MouseTracker>
  {({ x, y }) => (
    <p>The mouse is at {x}, {y}.</p>
  )}
</MouseTracker>

children is being used as a function here rather than as JSX, which is the defining trait of a render prop — the prop doesn’t have to be named children; render is another common name for it. The logic (tracking mouse position) and the rendering (what to show) are decoupled, which is exactly what makes it reusable: a second caller could render a crosshair instead of text, with zero changes to MouseTracker.

Higher-order components: wrapping a component in behavior

A HOC takes a component as input and returns a new one that wraps it:

function withMousePosition(WrappedComponent) {
  return function ComponentWithMouse(props) {
    const [position, setPosition] = useState({ x: 0, y: 0 });

    function handleMouseMove(event) {
      setPosition({ x: event.clientX, y: event.clientY });
    }

    return (
      <div onMouseMove={handleMouseMove}>
        <WrappedComponent {...props} mousePosition={position} />
      </div>
    );
  };
}

function Cursor({ mousePosition }) {
  return <p>The mouse is at {mousePosition.x}, {mousePosition.y}.</p>;
}

const CursorWithMouse = withMousePosition(Cursor);

withMousePosition injects a mousePosition prop into whatever component it wraps. The naming convention withX signals a HOC the same way a use prefix signals a hook. Several HOCs can be composed by wrapping one inside another, though that quickly produces deeply nested wrapper trees and prop names that can silently collide if two HOCs both try to inject a prop called, say, data.

Why hooks mostly replaced both

A custom hook achieves the same sharing goal with none of the structural cost:

function useMousePosition() {
  const [position, setPosition] = useState({ x: 0, y: 0 });

  useEffect(() => {
    function handleMouseMove(event) {
      setPosition({ x: event.clientX, y: event.clientY });
    }
    window.addEventListener("mousemove", handleMouseMove);
    return () => window.removeEventListener("mousemove", handleMouseMove);
  }, []);

  return position;
}

function Cursor() {
  const { x, y } = useMousePosition();
  return <p>The mouse is at {x}, {y}.</p>;
}

No extra component wraps Cursor in the rendered tree, no function nests inside JSX, and nothing can collide on a prop name since the hook just returns a plain value the component destructures itself. This is why, honestly, render props and HOCs stopped being the default choice for new code almost as soon as hooks shipped — they solved a real problem that hooks solve more directly.

They have not vanished. Some long-lived libraries still expose a withX HOC or a render-prop component in their public API because reworking that API would be a breaking change, and you will still meet both patterns reading code written before 2019 or so. Recognizing the shape — a function prop called with internal state, or a function that returns a wrapped component — is what lets you use those libraries and read that code without being confused by it.

What the video covers

Developer Way’s Render props episode, from Nadia Makarevich’s Advanced React course, explains the render-prop pattern and explicitly addresses how hooks changed the picture and when render props can still earn their place today — the same honest framing this lesson takes. It covers render props only; the HOC half of this lesson’s scope, and the worked withMousePosition example above, go beyond what that video covers, which is why Cosden Solutions’ HOC-focused video is linked as a deeper resource.

Where this fits

Custom hooks are the pattern that replaced most uses of render props and HOCs, so they deserve a full lesson of their own. Next: custom hooks.

Check yourself

  1. 01What does a render prop component actually render?

    Whatever the function passed as its prop returns. The component manages some state or behavior internally and calls that function with the current value, handing the caller full control over the output markup.

  2. 02What does a higher-order component return?

    A new component. The HOC is a plain function that takes a component as its argument and returns a different component, typically one that renders the original with some additional props or wrapped in additional behavior.

  3. 03Why do most new React components reach for a custom hook instead of a render prop or an HOC for shared stateful logic?

    A custom hook shares the same logic without adding an extra component to the tree, without the nested-function indentation a render prop produces, and without the prop-name collisions that can happen when multiple HOCs wrap the same component.

  4. 04Why do render props or HOCs still appear in real code today?

    Some library APIs were built around one of these patterns before hooks existed and have not been reworked, so using that library means working with its render-prop or HOC-based API even in an otherwise hooks-based app.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark