React hooks with TypeScript

Brandon S. Ha
Sep 15, 2021
  1. useState

we don’t really define the types in useState.
import useState from react

type {
name: string;
email: string
}
const [isLoggedIn, setIsLoggedIn] = useState< AuthUser |null>(null);

whatever we put the initial value, we have to stick to it. For example, the above example is defined with boolean type. Now we can inform TS that the type of User can be null or AuthUser.

2. useReducer

--

--