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

Re-rendering with rerender

Testing component updates by calling rerender with new props instead of rendering from scratch.

Re-rendering with rerender

You'll learn how to test prop changes by updating an already-rendered component instead of rendering it again from scratch.

The Core Idea

When you call render() from RTL, it returns an object containing several utilities—including a rerender function. This function lets you update the props of the component you just rendered without mounting a fresh instance. This mirrors what happens in real React apps: when a parent changes props, the child re-renders with new data rather than unmounting and remounting.

Why This Matters

Imagine testing a <Counter count={5} /> component that displays different messages based on the count prop. You could call render() multiple times with different props, but that creates separate component instances. Using rerender tests the actual update lifecycle—the same component instance receiving new props, preserving internal state and refs.

How It Works

const { rerender } = render(<Greeting name="Alice" />);
expect(screen.getByText(/Hello, Alice/i)).toBeInTheDocument();

// Update the prop on the same component instance
rerender(<Greeting name="Bob" />);
expect(screen.getByText(/Hello, Bob/i)).toBeInTheDocument();

The rerender function accepts a new React element—typically the same component with different props. RTL updates the existing component rather than tearing it down and rebuilding it.

The Analogy

Think of it like editing a document: render() opens a new file, while rerender() saves changes to the file you already have open. You're testing how the component responds to change, not how it initializes multiple times.

Key Takeaway: Use rerender to test prop updates on the same component instance, simulating real React update behavior instead of mounting from scratch.