@@ -11,16 +11,14 @@ route: '/reference/api'
11
11
- [ ` act ` ] ( /reference/api#act )
12
12
- [ ` cleanup ` ] ( /reference/api#cleanup )
13
13
- [ ` addCleanup ` ] ( /reference/api#addcleanup )
14
+ - [ ` removeCleanup ` ] ( /reference/api#removecleanup )
14
15
15
16
---
16
17
17
18
## ` renderHook `
18
19
19
- ``` js
20
- function renderHook (
21
- callback : function (props ?: any ): any,
22
- options?: RenderHookOptions
23
- ): RenderHookResult
20
+ ``` ts
21
+ function renderHook(callback : (props ? : any ) => any , options ? : RenderHookOptions ): RenderHookResult
24
22
```
25
23
26
24
Renders a test component that will call the provided ` callback ` , including any hooks it calls , every
@@ -61,7 +59,7 @@ The `renderHook` function returns an object that has the following properties:
61
59
62
60
### ` result `
63
61
64
- ```js
62
+ ` ` ` ts
65
63
{
66
64
all: Array<any>
67
65
current: any,
@@ -77,7 +75,7 @@ returned at the time.
77
75
78
76
### ` rerender `
79
77
80
- ` ` ` js
78
+ ` ` ` ts
81
79
function rerender(newProps?: any): void
82
80
` ` `
83
81
@@ -86,13 +84,27 @@ passed, they will replace the `callback` function's `initialProps` for subsequen
86
84
87
85
### ` unmount `
88
86
89
- ```js
87
+ ` ` ` ts
90
88
function unmount(): void
91
89
` ` `
92
90
93
91
A function to unmount the test component. This is commonly used to trigger cleanup effects for
94
92
`useEffect` hooks.
95
93
94
+ ### ` hydrate `
95
+
96
+ ` ` ` ts
97
+ function hydrate(): void
98
+ ` ` `
99
+
100
+ > This is only used when using the ` server ` module . See [SSR ](/ usage / ssr ) for more information on
101
+ > server - side rendering your hooks .
102
+
103
+ A function to hydrate a server rendered component into the DOM. This is required before you can
104
+ interact with the hook, whether that is an `act` or `rerender` call. Effects created using
105
+ `useEffect` or `useLayoutEffect` are also not run on server rendered hooks until `hydrate` is
106
+ called.
107
+
96
108
### ` ...asyncUtils `
97
109
98
110
Utilities to assist with testing asynchronous behaviour . See the
@@ -102,15 +114,15 @@ Utilities to assist with testing asynchronous behaviour. See the
102
114
103
115
## ` act `
104
116
105
- This is the same [`act` function](https:// reactjs.org/docs/test-utils.html#act) that is exported by
106
- `react-test- renderer` .
117
+ This is the same [` act ` function ](https : // reactjs.org/docs/test-utils.html#act) function that is
118
+ exported from your [ chosen renderer ](/ installation # renderer ) .
107
119
108
120
---
109
121
110
122
## ` cleanup `
111
123
112
- ```js
113
- function cleanup: Promise<void>
124
+ ` ` ` ts
125
+ function cleanup() : Promise<void>
114
126
` ` `
115
127
116
128
Unmounts any rendered hooks rendered with ` renderHook ` , ensuring all effects have been flushed. Any
@@ -142,7 +154,8 @@ module.exports = {
142
154
` ` `
143
155
144
156
Alternatively , you can change your test to import from ` @testing-library/react-hooks/pure ` instead
145
- of the regular imports.
157
+ of the regular imports. This applys to any of our export methods documented in
158
+ [Rendering ](/installation #being -specific ).
146
159
147
160
` ` ` diff
148
161
- import { renderHook, cleanup, act } from '@testing-library/react-hooks'
@@ -156,8 +169,8 @@ variable to `true` before importing `@testing-library/react-hooks` will also dis
156
169
157
170
## ` addCleanup `
158
171
159
- ` ` ` js
160
- function addCleanup (callback : function (): void| Promise<void>): function (): void
172
+ ` ` ` ts
173
+ function addCleanup(callback: () => void | Promise<void>): (): void
161
174
` ` `
162
175
163
176
Add a callback to be called during [` cleanup ` ](/reference /api #cleanup ), returning a function to
@@ -173,8 +186,8 @@ be resolved before moving onto the next cleanup callback.
173
186
174
187
## `removeCleanup`
175
188
176
- ```js
177
- function removeCleanup(callback: function(): void| Promise<void>): void
189
+ ```ts
190
+ function removeCleanup(callback : () => void | Promise <void >): void
178
191
```
179
192
180
193
Removes a cleanup callback previously added with [`addCleanup`](/reference /api #addCleanup ). Once
@@ -187,10 +200,8 @@ removed, the provided callback will no longer execute as part of running
187
200
188
201
### `waitForNextUpdate`
189
202
190
- ```js
191
- function waitForNextUpdate(options?: {
192
- timeout?: number
193
- }): Promise<void>
203
+ ```ts
204
+ function waitForNextUpdate(options ? : { timeout? : number }): Promise <void >
194
205
```
195
206
196
207
Returns a `Promise` that resolves the next time the hook renders, commonly when state is updated as
@@ -202,12 +213,15 @@ The maximum amount of time in milliseconds (ms) to wait. By default, no timeout
202
213
203
214
### `waitFor`
204
215
205
- ```js
206
- function waitFor(callback: function(): boolean|void, options?: {
207
- interval?: number,
208
- timeout?: number,
209
- suppressErrors?: boolean
210
- }): Promise<void>
216
+ ```ts
217
+ function waitFor(
218
+ callback : () => boolean | void ,
219
+ options ? : {
220
+ interval? : number
221
+ timeout? : number
222
+ suppressErrors? : boolean
223
+ }
224
+ ): Promise <void >
211
225
```
212
226
213
227
Returns a `Promise` that resolves if the provided callback executes without exception and returns a
@@ -232,12 +246,15 @@ rejected. By default, errors are suppressed for this utility.
232
246
233
247
### `waitForValueToChange`
234
248
235
- ```js
236
- function waitForValueToChange(selector: function(): any, options?: {
237
- interval?: number,
238
- timeout?: number,
239
- suppressErrors?: boolean
240
- }): Promise < void >
249
+ ```ts
250
+ function waitForValueToChange(
251
+ selector : () => any ,
252
+ options ? : {
253
+ interval? : number
254
+ timeout? : number
255
+ suppressErrors? : boolean
256
+ }
257
+ ): Promise <void >
241
258
```
242
259
243
260
Returns a `Promise` that resolves if the value returned from the provided selector changes. It is
0 commit comments