renderHook function
renderHook
Renders a test component that will call the provided callback, including any hooks it calls, every time it renders. Returns RenderHookResult object, which you can interact with.
The renderHook function accepts the following arguments:
Callback is a function that is called each render of the test component. This function should call one or more hooks for testing.
The props passed into the callback will be the initialProps provided in the options to renderHook, unless new props are provided by a subsequent rerender call.
options
A RenderHookOptions<Props> object to modify the execution of the callback function, containing the following properties:
initialProps
The initial values to pass as props to the callback function of renderHook. The Props type is determined by the type passed to or inferred by the renderHook call.
wrapper
A React component to wrap the test component in when rendering. This is usually used to add context providers from React.createContext for the hook to access with useContext.
concurrentRoot
Set to false to disable concurrent rendering.
Otherwise, render will default to using concurrent rendering used in the React Native New Architecture.
Result
The renderHook function returns an object that has the following properties:
result
The current value of the result will reflect the latest of whatever is returned from the callback passed to renderHook. The Result type is determined by the type passed to or inferred by the renderHook call.
rerender
A function to rerender the test component, causing any hooks to be recalculated. If newProps are passed, they will replace the callback function's initialProps for subsequent rerenders. The Props type is determined by the type passed to or inferred by the renderHook call.
unmount
A function to unmount the test component. This is commonly used to trigger cleanup effects for useEffect hooks.
Examples
Here we present some extra examples of using renderHook API.
With initialProps
With wrapper
renderHookAsync function
Async versions of renderHook designed for working with React 19 and React Suspense. This method uses async act function internally to ensure all pending React updates are executed during rendering.
- Returns a Promise: Should be awaited
- Async methods: Both
rerenderandunmountreturn Promises and should be awaited - Suspense support: Compatible with React Suspense boundaries and
React.use()
Result
The RenderHookAsyncResult differs from RenderHookResult in that rerenderAsync and unmountAsync are async functions.
Use renderHookAsync when testing hooks that use React Suspense, React.use(), or other concurrent features where timing of re-renders matters.
