Course contentsShow
← React
Lesson 1066 of 2,39627. Composition: Render PropsFree lesson

Render Props for Legacy Integration

Wrapping class components or third-party libraries with render props provides a hooks-compatible interface.

What You'll Learn

You'll learn how to use render props to wrap older class-based components or third-party libraries that don't support hooks, giving them a modern, hooks-compatible interface.

The Integration Challenge

React's ecosystem contains plenty of legacy code: class components from pre-2019 projects, third-party libraries that haven't updated to hooks, or components you can't refactor yet. These don't work directly with hooks, but your new code does. Render props act as a translation layer — like an adapter plug that lets you use an old appliance with a new electrical outlet.

How Render Props Bridge the Gap

A render prop component can wrap legacy logic (class methods, lifecycle methods, or non-hook libraries) and expose that logic to functional components via a render function. The wrapper component handles the "old way," while the render prop passes clean, modern data to your hook-based UI.

// Wrap a legacy class component with render props
function LegacyDataAdapter({ render }) {
  const [data, setData] = React.useState(null);
  
  // Legacy library or class logic lives here
  React.useEffect(() => {
    const legacyInstance = new OldLibrary();
    legacyInstance.onData(setData);
    return () => legacyInstance.cleanup();
  }, []);
  
  return render(data);
}

// Now your modern components consume it cleanly
function ModernComponent() {
  return (
    <LegacyDataAdapter render={data => (
      <div>{data ? data.value : 'Loading...'}</div>
    )} />
  );
}

This pattern lets you gradually modernize a codebase without rewriting everything at once.

When This Pattern Shines

Use render props for legacy integration when you need stateful logic from class components, third-party libraries that don't export hooks, or components in a large refactor where you're converting section by section.

Key Takeaway: Render props let you wrap legacy class components or old libraries in a hooks-compatible interface, providing a smooth migration path without forcing immediate rewrites.