Skip to content

Commit 3fa42d5

Browse files
authored
docs(renderHook): remove references to error and all from renderHook API (#963)
* docs(renderHook): remove references to error and all from renderHook API * docs(renderHook): update code snippets to use TS
1 parent 480a38e commit 3fa42d5

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

Diff for: website/docs/API.md

+21-19
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ test('waiting for an Banana to be removed', async () => {
400400
});
401401
```
402402

403-
This method expects that the element is initally present in the render tree and then is removed from it. If the element is not present when you call this method it throws an error.
403+
This method expects that the element is initially present in the render tree and then is removed from it. If the element is not present when you call this method it throws an error.
404404

405405
You can use any of `getBy`, `getAllBy`, `queryBy` and `queryAllBy` queries for `expectation` parameter.
406406

@@ -471,10 +471,10 @@ Useful function to help testing components that use hooks API. By default any `r
471471
Defined as:
472472

473473
```ts
474-
function renderHook(
475-
callback: (props?: any) => any,
476-
options?: RenderHookOptions
477-
): RenderHookResult;
474+
function renderHook<Result, Props>(
475+
callback: (props?: Props) => Result,
476+
options?: RenderHookOptions<Props>
477+
): RenderHookResult<Result, Props>;
478478
```
479479

480480
Renders a test component that will call the provided `callback`, including any hooks it calls, every time it renders. Returns [`RenderHookResult`](#renderhookresult-object) object, which you can interact with.
@@ -515,41 +515,43 @@ The `props` passed into the callback will be the `initialProps` provided in the
515515

516516
### `options` (Optional)
517517

518-
A `RenderHookOptions` object to modify the execution of the `callback` function, containing the following properties:
518+
A `RenderHookOptions<Props>` object to modify the execution of the `callback` function, containing the following properties:
519519

520520
#### `initialProps`
521521

522-
The initial values to pass as `props` to the `callback` function of `renderHook`.
522+
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.
523523

524524
#### `wrapper`
525525

526-
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`. `initialProps` and props subsequently set by `rerender` will be provided to the wrapper.
526+
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`.
527527

528-
### `RenderHookResult` object
528+
### `RenderHookResult<Result, Props>` object
529529

530530
The `renderHook` function returns an object that has the following properties:
531531

532532
#### `result`
533533

534534
```jsx
535535
{
536-
all: Array<any>
537-
current: any,
538-
error: Error
536+
current: Result
539537
}
540538
```
541539

542-
The `current` value of the `result` will reflect the latest of whatever is returned from the `callback` passed to `renderHook`. Any thrown values from the latest call will be reflected in the `error` value of the `result`. The `all` value is an array containing all the returns (including the most recent) from the callback. These could be `result` or an `error` depending on what the callback returned at the time.
540+
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.
543541

544542
#### `rerender`
545543

546-
function rerender(newProps?: any): void
544+
```ts
545+
function rerender(newProps?: Props): void;
546+
```
547547

548-
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.
548+
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.
549549

550550
#### `unmount`
551551

552-
function unmount(): void
552+
```ts
553+
function unmount(): void;
554+
```
553555

554556
A function to unmount the test component. This is commonly used to trigger cleanup effects for `useEffect` hooks.
555557

@@ -559,7 +561,7 @@ Here we present some extra examples of using `renderHook` API.
559561

560562
#### With `initialProps`
561563

562-
```jsx
564+
```ts
563565
const useCount = (initialCount: number) => {
564566
const [count, setCount] = useState(initialCount);
565567
const increment = () => setCount((previousCount) => previousCount + 1);
@@ -591,8 +593,8 @@ it('should increment count', () => {
591593

592594
#### With `wrapper`
593595

594-
```jsx
595-
it('should work properly', () => {
596+
```tsx
597+
it('should use context value', () => {
596598
function Wrapper({ children }: { children: ReactNode }) {
597599
return <Context.Provider value="provided">{children}</Context.Provider>;
598600
}

0 commit comments

Comments
 (0)