typescript-cheatsheets / react

Cheatsheets for experienced React developers getting started with TypeScript

Home Page:https://react-typescript-cheatsheet.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Basic] Hooks: useState type

priscilaandreani opened this issue · comments

What cheatsheet is this about? (if applicable)

Basic cheatsheet

What's your issue or idea?
Idea

instead of:

const [user, setUser] = React.useState<IUser | null>(null);

// later...
setUser(newUser);

you can use:

const [user, setUser] = React.useState<IUser>({} as IUser);

// later...
setUser(newUser);

(of course if IUser is an object)

its always great to start the state using its initial format

thanks @priscilaandreani, i've added this comment now! https://github.com/typescript-cheatsheets/react/blob/main/docs/basic/getting-started/hooks.md#usestate

happy to take more suggestions from you :)