Writing custom React hooks with Typescript isn’t all that different from writing them in plain old JavaScript. In this post, we review some basics and “gotchas” of writing your own React hooks with Typescript.
To demonstrate how to write our hooks, we’ll write a hook that does something pretty ubiquitous in web applications: fetching data. Our hook will be called useFetch
.
Importantly, we’re just going through the basics here, so this will be a very bare-bones hook. If you want to actually use a useFetch
hook in production, I recommend googling around for an existing one that has a lot more bells and whistles.
#react