You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/api.md
+77-17
Original file line number
Diff line number
Diff line change
@@ -6,33 +6,93 @@ route: '/reference/api'
6
6
7
7
# API
8
8
9
-
## `renderHook(callback[, options])`
9
+
`react-hooks-testing-library` exports the following methods:
10
+
11
+
-[`renderHook`](/reference/api#renderhook)
12
+
-[`act`](/reference/api#act)
13
+
14
+
---
15
+
16
+
## `renderHook`
17
+
18
+
```js
19
+
functionrenderHook(
20
+
callback:function(props?:any): any,
21
+
options?: RenderHookOptions
22
+
): RenderHookResult
23
+
```
10
24
11
25
Renders a test component that will call the provided `callback`, including any hooks it calls, every time it renders.
12
26
27
+
The `renderHook` function accept the following arguments:
28
+
29
+
### `callback`
30
+
31
+
The function that is called each `render` of the test component. This function should call one or more hooks for testing.
32
+
33
+
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.
34
+
35
+
### `options`
36
+
37
+
An options object to modify the execution of the `callback` function. See the [`renderHook` Options](/reference/api#renderhook-options) section for more details.
38
+
13
39
> _Note: `testHook` has been renamed to `renderHook`. `testHook` will continue work in the current version with a deprecation warning, but will be removed in a future version._
14
40
>
15
41
> **_You should update any usages of `testHook` to use `renderHook` instead._**
16
42
17
-
### Arguments
43
+
## `renderHook` Options
44
+
45
+
The `renderHook` function accepts the following options as the second parameter:
46
+
47
+
### `initialProps`
48
+
49
+
The initial values to pass as `props` to the `callback` function of `renderHook.
50
+
51
+
### `wrapper`
18
52
19
-
-`callback` (`function([props])`) - function to call each render. This function should call one or more hooks for testing
20
-
- The `props` passed into the callback will be the `initialProps` provided in the `options` until new props are provided by a `rerender` call
21
-
-`options` (`object`)
22
-
-`initialProps` (`object`) - the initial values to pass to the `callback` function
23
-
-`wrapper` (`component`) - pass a React component to wrap the test component
24
-
- This is usually used to add context providers from `React.createContext` for the hook access with `useContext`
53
+
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`.
25
54
26
-
### Returns
55
+
## `renderHook` Result
56
+
57
+
The `renderHook` method returns an object that has a following properties:
58
+
59
+
### `result`
60
+
61
+
```js
62
+
{
63
+
current: any,
64
+
error:Error
65
+
}
66
+
```
67
+
68
+
The `current` value or the `result` will reflect whatever is returned from the `callback` passed to `renderHook`. Any thrown values will be reflected in the `error` value of the `result`.
69
+
70
+
### `waitForNextUpdate`
71
+
72
+
```js
73
+
functionwaitForNextUpdate(): Promise<void>
74
+
```
27
75
28
-
-`result` (`object`)
29
-
-`current` (`any`) - the return value of the `callback` function
30
-
-`error` (`Error`) - the error that was thrown if the `callback` function threw an error during rendering
31
76
- `waitForNextUpdate` (`function`) - returns a `Promise` that resolves the next time the hook renders, commonly when state is updated as the result of a asynchronous action
32
-
-`rerender` (`function([newProps])`) - function to rerender the test component including any hooks called in the `callback` function
33
-
- If `newProps` are passed, the will replace the `initialProps` passed the the `callback` function for future renders
34
-
-`unmount` (`function()`) - function to unmount the test component, commonly used to trigger cleanup effects for `useEffect` hooks
35
77
36
-
## `act(callback)`
78
+
### `rerender`
79
+
80
+
```js
81
+
function rerender(newProps?: any): void
82
+
```
83
+
84
+
A function to rerender the test component, causing any hooks to be recalculated. If `newProps` are passed, the will replace the `initialProps` passed to the `callback` function for the rerender any subsequent renders.
85
+
86
+
### `unmount`
87
+
88
+
```js
89
+
function unmount(): void
90
+
```
91
+
92
+
A function to unmount the test component. This is commonly used to trigger cleanup effects for `useEffect` hooks.
93
+
94
+
---
95
+
96
+
## `act`
37
97
38
-
This is the same [`act` function](https://testing-library.com/docs/react-testing-library/api#act) that is exported by `react-testing-library`.
98
+
This is the same [`act` function](https://reactjs.org/docs/test-utils.html#act) that is exported by `react-test-renderer`.
0 commit comments