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

Testing Hook Updates with rerender

Calling the rerender function to test how hooks respond to prop changes over time.

Testing Hook Updates with rerender

You'll learn how to simulate prop changes in isolated hook tests to verify your custom hooks respond correctly over time.

When testing custom hooks in isolation, you often need to verify how they react to changing inputs—new props passed by a parent component. The rerender function from RTL's renderHook lets you do exactly that: re-execute your hook with different arguments without mounting a whole new component.

The Core Pattern

After calling renderHook(), you get back a rerender function. Call it with new arguments, and your hook runs again with those updated values—just like React would do when props change in a real component:

const { result, rerender } = renderHook(
  ({ count }) => useDoubledValue(count),
  { initialProps: { count: 5 } }
);

expect(result.current).toBe(10);

// Simulate a prop change
rerender({ count: 8 });

expect(result.current).toBe(16);

Why This Matters

Custom hooks often maintain internal state or effects that depend on props. A hook might cache values, trigger API calls, or update subscriptions when inputs change. Without rerender, you'd only test the first render—missing bugs that appear during updates.

Think of rerender as a time machine: you're fast-forwarding to a moment when the parent component passes different props, letting you verify your hook's state transitions, cleanup logic, and derived values update correctly.

// Testing a hook with effects
const { result, rerender } = renderHook(
  ({ userId }) => useFetchUser(userId),
  { initialProps: { userId: 1 } }
);

await waitFor(() => expect(result.current.loading).toBe(false));

// User navigates to different profile
rerender({ userId: 2 });

expect(result.current.loading).toBe(true); // Should refetch

Key Takeaway: Use rerender to test how your custom hooks handle prop changes, verifying state updates, effect cleanup, and derived values across the hook's entire lifecycle.