renderHook
functionRenders 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
.
RenderHookResult
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.
Here we present some extra examples of using renderHook
API.
initialProps
wrapper