Course contentsShow
← React
Lesson 2096 of 2,39646. Testing React with RTLFree lesson

The renderHook Function from RTL

How renderHook wraps your hook in a test component and provides a stable testing interface.

The renderHook Function from RTL

You'll learn how renderHook wraps your custom hook in a minimal test component so you can test its behavior without needing a real component at all.

The Problem It Solves

Custom hooks can't be called directly in tests — React rules forbid calling hooks outside of components. You could create a dummy component just to call your hook, but that's tedious boilerplate for every test.

What renderHook Does

Think of renderHook as a invisible test harness. It creates a tiny, throwaway component behind the scenes, calls your hook inside it, and then gives you a stable interface to examine what the hook returns. You never see or care about that wrapper component — you just interact with your hook's API.

import { renderHook } from '@testing-library/react';
import { useCounter } from './useCounter';

test('hook returns initial count', () => {
  const { result } = renderHook(() => useCounter(0));
  
  expect(result.current).toEqual({ count: 0, increment: expect.any(Function) });
});

The result.current Pattern

The result object has a .current property that always points to your hook's latest return value. It's wrapped this way because hooks can update over time — result.current gives you a stable reference that automatically reflects the newest state after re-renders.

This means you don't manually manage React's render cycle. RTL handles the component lifecycle, and you just read from result.current whenever you need to check the hook's output.

The Interface You Get

renderHook returns an object with result, rerender, and unmount. You'll primarily use result.current to access what your hook returns. The other methods let you test re-renders and cleanup, which you'll encounter in subsequent lessons.

Key Takeaway: renderHook eliminates boilerplate by automatically wrapping your hook in a test component, giving you direct access to its return value through result.current.