Skip to content

Commit

Permalink
docs(hooks): improving useImperativeHandle example (typescript-cheats…
Browse files Browse the repository at this point in the history
…heets#542)

* docs(hooks): improving useImperativeHandle example

* Update hooks.md

* chore(readme): running codegen

* chore: formatting files
  • Loading branch information
mateoguzmana authored Sep 4, 2022
1 parent 9ec8261 commit 2859208
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 16 deletions.
47 changes: 39 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,22 +559,53 @@ function Foo() {

#### useImperativeHandle

_We don't have much here, but this is from [a discussion in our issues](https://github.com/typescript-cheatsheets/react/issues/106). Please contribute if you have anything to add!_
Based on this [Stackoverflow answer](https://stackoverflow.com/a/69292925/5415299):

```tsx
type ListProps<ItemType> = {
items: ItemType[];
innerRef?: React.Ref<{ scrollToItem(item: ItemType): void }>;
// Countdown.tsx

// Define the handle types which will be passed to the forwardRef
export type CountdownHandle = {
start: () => void;
};

function List<ItemType>(props: ListProps<ItemType>) {
useImperativeHandle(props.innerRef, () => ({
scrollToItem() {},
type CountdownProps = {};

const Countdown = forwardRef<CountdownHandle, CountdownProps>((props, ref) => {
useImperativeHandle(ref, () => ({
// start() has type inference here
start() {
alert("Start");
},
}));
return null;

return <div>Countdown</div>;
});
```

```tsx
// The component uses the Countdown component

import Countdown, { CountdownHandle } from "./Countdown.tsx";

function App() {
const countdownEl = useRef<CountdownHandle>(null);

useEffect(() => {
if (countdownEl.current) {
// start() has type inference here as well
countdownEl.current.start();
}
}, []);

return <Countdown ref={countdownEl} />;
}
```

##### See also:

- [Using ForwardRefRenderFunction](https://stackoverflow.com/a/62258685/5415299)

#### Custom Hooks

If you are returning an array in your Custom Hook, you will want to avoid type inference as TypeScript will infer a union type (when you actually want different types in each position of the array). Instead, use [TS 3.4 const assertions](https://devblogs.microsoft.com/typescript/announcing-typescript-3-4/#const-assertions):
Expand Down
47 changes: 39 additions & 8 deletions docs/basic/getting-started/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,53 @@ function Foo() {

## useImperativeHandle

_We don't have much here, but this is from [a discussion in our issues](https://github.com/typescript-cheatsheets/react/issues/106). Please contribute if you have anything to add!_
Based on this [Stackoverflow answer](https://stackoverflow.com/a/69292925/5415299):

```tsx
type ListProps<ItemType> = {
items: ItemType[];
innerRef?: React.Ref<{ scrollToItem(item: ItemType): void }>;
// Countdown.tsx

// Define the handle types which will be passed to the forwardRef
export type CountdownHandle = {
start: () => void;
};

function List<ItemType>(props: ListProps<ItemType>) {
useImperativeHandle(props.innerRef, () => ({
scrollToItem() {},
type CountdownProps = {};

const Countdown = forwardRef<CountdownHandle, CountdownProps>((props, ref) => {
useImperativeHandle(ref, () => ({
// start() has type inference here
start() {
alert("Start");
},
}));
return null;

return <div>Countdown</div>;
});
```

```tsx
// The component uses the Countdown component

import Countdown, { CountdownHandle } from "./Countdown.tsx";

function App() {
const countdownEl = useRef<CountdownHandle>(null);

useEffect(() => {
if (countdownEl.current) {
// start() has type inference here as well
countdownEl.current.start();
}
}, []);

return <Countdown ref={countdownEl} />;
}
```

### See also:

- [Using ForwardRefRenderFunction](https://stackoverflow.com/a/62258685/5415299)

## Custom Hooks

If you are returning an array in your Custom Hook, you will want to avoid type inference as TypeScript will infer a union type (when you actually want different types in each position of the array). Instead, use [TS 3.4 const assertions](https://devblogs.microsoft.com/typescript/announcing-typescript-3-4/#const-assertions):
Expand Down

0 comments on commit 2859208

Please sign in to comment.