Skip to main content

Lesson 2 of 8

Render Props & Children as Functions

Render props pass a function as a prop (or as children), receiving internal state and returning JSX. This pattern completely decouples logic from presentation - the parent declares behavior, the consumer decides what to render with that data.

25 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Render Props Pattern

Render Props Pattern
import { useState, useEffect, useRef, type ReactNode } from 'react';

// Hover state render prop
interface HoverableProps {
  children: (isHovered: boolean) => ReactNode;
}

function Hoverable({ children }: HoverableProps) {
  const [isHovered, setIsHovered] = useState(false);
  return (
    <div
      onMouseEnter={() => setIsHovered(true)}
      onMouseLeave={() => setIsHovered(false)}
    >
      {children(isHovered)} {/* invoke with state */}
    </div>
  );
}

// Usage - consumer decides what to render
<Hoverable>
  {isHovered => (
    <div style={{ background: isHovered ? '#229c6b' : '#1e293b' }}>
      {isHovered ? '✨ Hovered!' : 'Hover me'}
    </div>
  )}
</Hoverable>

// Intersection Observer render prop
interface InViewProps {
  children: (inView: boolean, ref: React.RefCallback<Element>) => ReactNode;
  threshold?: number;
}

function InView({ children, threshold = 0.5 }: InViewProps) {
  const [inView, setInView] = useState(false);

  const setRef = (element: Element | null) => {
    if (!element) return;
    const observer = new IntersectionObserver(
      ([entry]) => setInView(entry.isIntersecting),
      { threshold }
    );
    observer.observe(element);
    return () => observer.disconnect();
  };

  return <>{children(inView, setRef)}</>;
}

// Usage - lazy-animate content as it enters viewport
<InView threshold={0.2}>
  {(inView, ref) => (
    <section
      ref={ref}
      style={{
        opacity: inView ? 1 : 0,
        transform: inView ? 'translateY(0)' : 'translateY(20px)',
        transition: 'all 0.6s',
      }}
    >
      Animated content
    </section>
  )}
</InView>

// NOTE: In modern React, custom hooks (useHover, useInView) 
// are preferred over render props because they:
// - Don't add JSX nesting
// - Can be composed more easily
// - Have better TypeScript inference
// Render props still have a place for JSX-heavy cases

Tip

Tip

Practice Render Props Children as Functions in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and flow for Render Props & Children as Functions

Practice Task

Note

Practice Task - (1) Write a working example of Render Props Children as Functions from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Render Props Children as Functions is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done