Skip to content

Commit

Permalink
Create ts-react-elementref.md
Browse files Browse the repository at this point in the history
  • Loading branch information
petermekhaeil authored Jan 3, 2024
1 parent b24ab1d commit b595d57
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions learnings/ts-react-elementref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TypeScript: ElementRef for React.useRef

You can extract the type from a `useRef` hook using `ElementRef`:

```tsx
import { useRef, ElementRef } from "react";

const Component = () => {
const audioRef = useRef<ElementRef<"audio">>(null);
// ^? React.RefObject<HTMLAudioElement>

return <audio ref={audioRef}>Hello</audio>;
};
```

Read [Matt Pocock's Strongly Type useRef with ElementRef](https://www.totaltypescript.com/strongly-type-useref-with-elementref).

0 comments on commit b595d57

Please sign in to comment.