TypeScript is a great language that allows type-checking your code in order to make it more robust and understandable.
In this guide, I will lead you in setting up TypeScript types on React hooks (useState, useContext, useCallback, and so on).
Sorry for the interrupt!
If you ever wanted to learn Typescript and integrate it into your React project and build custom components taking full advantage of TypeScript and React and just can’t find the time or dreading. The one-page guide to TypeScript: usage, examples, links, snippets, and more. This is Devhints.io cheatsheets — a collection of cheatsheets I've written.
If you're interested in learning TypeScript or React in a comprehensive way, I highly recommend these bestseller courses:
> Understanding TypeScript - 2020 Edition
> React - The Complete Guide (incl Hooks, React Router, Redux)
Let's dive in
Set types on useState
The useState
hook allows you to manage state in your React app. It's the equivalent of this.state
in a Class component.
To set types on useState
hook, you need to pass into <>
the type of the state. You can also use union type like this <number | null>
if you don't have an initial state.
Set types on useRef
The useRef
hook returns a mutable ref object that allows accessing DOM elements.
As you can see, the way useRef
receives types is the same as the useState
hook. You just have to pass it into the <>
- and, if you have multiple type annotations, just use union type as I do here.
Set types on useContext
useContext
is a hook that allows accessing and consuming a given Context in a React app.
Here, we start by creating the IArticle
interface that is the type of our context.Next, we use it on the createContext()
method to create a new context, and then initialize it with []
- you can also use null
as an initial state if you want too.
With that in place, we can now handle the state of the context and set the type on useContext
in order to expect an array of type IArticle
as value.
Set types on useReducer
The useReducer
hook helps to manage more complex states. Download excel 2013 for mac. It's an alternative to useState
- but keep in mind that they are different.
Here, we start by declaring the action types that allow handling the counter. Next, we set respectively two types for the reducer function and the counter state.
The reducer expects a state
of type ICounter
and an action
of type IReducer
. With that, the counter can now be handle consequently.
The useReducer
hook receives the reducer function and an initial state as arguments and returns two elements: the state
of the counter and the dispatch
action.
To set the type for the values returned by ueReducer
- just pass into the <>
the type of your data.
With that in place, the counter can now be incremented or decremented through useReducer
.
Set types on useMemo
The useMemo
hook allows you to memorize the output of a given function. It returns a memoized value.
To set types on useMemo
- just pass into the <>
the type of data you want to memorize.Here, the hook expects a string
as a returned value.
Set types on useCallback
The useCallback
hook allows you to memorize a function to prevent unnecessary re-renders. It returns a memoized callback.
Here, we declare the CallbackType
type that is using as type on the callback we want to memorize.It expects to receive parameters of type string
and should return a value of type void
.
Next, we set that type on useCallback
- and if you pass a wrong type to the callback or the array of dependencies - TypeScript will yell at you.
Jsdoc Typescript Cheat Sheet
Thanks for reading
Ever since I started using TypeScript, I can't stop using it. Sometimes finding the correct type and where you should import it from can be a real headache. Especially when building a ReactJS application. This blog post is a great chance to publicly document my most used React TypeScript types. I focus on functional components and react hooks.
The structure of the article is that each paragraph is a standalone tip.
To create a React TypeScript project, we can use Create React App:

There have been lots of talks about the right way to import React. This is the most updated way to do it:
The return type of a functional component is ReactElement
If you want to extend the props of a native HTML element, you can use the generic class HTMLAttributes. Let's say I want to create a new button:
Note that we use destructuring to forward the props to the button element.
The children prop is of type ReactNode.
React's events system uses its own types and not the native HTML events. Make sure to import the event from the react library. import { MouseEvent } from 'react'.
Pass the correct type to the useRef generic. If you want to create a ref to an input element:
The ref.current type will automatically be HTMLInputElement.
Typescript Cheat Sheet Pdf
The same goes for useState.
If you provide an initial value in both cases, the type will be inferred implicitly.
When creating custom hooks make sure to explicitly set the returns type. Can i download turbotax for mac. Otherwise, TypeScript may infer incorrectly the types.

This is far from being a complete cheat sheet but rather documents the things I use the most. Check out this awesome cheat sheet for more information: https://github.com/typescript-cheatsheets/react.
React Ts
