Accessing Hook Results with result.current
You'll learn how to read the values returned by your custom hooks when testing them in isolation with renderHook.
The result Object Gateway
When you use renderHook() from React Testing Library, it doesn't return what your hook returns directly. Instead, it gives you a result object with a current property that holds your hook's return value.
Think of it like a snapshot frame: the result object is the camera, and result.current is the actual picture it captured at that moment. The camera stays the same, but the picture updates when your hook re-renders.
Reading Hook Return Values
After rendering a hook, access its return value through result.current:
import { renderHook } from '@testing-library/react';
import { useCounter } from './useCounter';
test('reads initial count', () => {
const { result } = renderHook(() => useCounter());
// result.current holds whatever useCounter returns
expect(result.current.count).toBe(0);
});
If your hook returns a single value instead of an object:
test('reads primitive return value', () => {
const { result } = renderHook(() => useToggle(false));
// result.current is the boolean itself
expect(result.current).toBe(false);
});
Why the Indirection?
The current property exists because hooks can trigger re-renders. RTL keeps the same result object reference but updates current when your hook's state changes. This lets you always access the latest values without losing the wrapper.
Key Takeaway: Always read your hook's return values through result.current — it's the live snapshot of what your hook currently returns, automatically updated after each re-render.