How to use useState hook in React with TypeScript correctly

0 votes

I am trying to make a login form in react with typescript. But setEmail method is not accepting value. It says Argument of type 'string' is not assignable to parameter of type 'SetStateAction'. What should I do to solve it?

enter image description here

Jun 10 in TypeSript by Logan
• 2,140 points
41 views

1 answer to this question.

0 votes

Without any initial argument, the type of email and setEmail will be undefined.

const [email, setEmail]: [undefined, (value: ((prevState: undefined) => undefined) | undefined) => void] = useState();

So the first step is to enforce email to be a string by either using useState("") or even better useState<string>("").

answered Jun 10 by Nina
• 3,020 points

Related Questions In TypeSript

0 votes
1 answer

How to use useState hook in React with typescript correctly?

You can set a string type for it Explicit way: const ...READ MORE

answered 5 days ago in TypeSript by Abhinaya
• 760 points
8 views
0 votes
1 answer

How to use document.getElementById() method in TypeScript?

Typescript will force you to check the ...READ MORE

answered Jun 15 in TypeSript by Nina
• 3,020 points
116 views
0 votes
1 answer

How to create ES6 Map in Typescript

Refer this as an example this.configs = new ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
22 views
0 votes
1 answer

How to declare and initialize a Dictionary in Typescript

Apparently this doesn't work when passing the ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
179 views
0 votes
1 answer

TypeScript - ',' expected.ts(1005)

You can't put statements in JSX curly braces, only expressions. You ...READ MORE

answered Jun 9 in TypeSript by Nina
• 3,020 points
436 views
0 votes
1 answer

Naming of TypeScript's union and intersection types

The type A | B refers to objects which ...READ MORE

answered Jun 10 in TypeSript by Nina
• 3,020 points
9 views
0 votes
0 answers

Create a react app react 18 with typescript

I am trying to create react 18 ...READ MORE

Jul 5 in TypeSript by Logan
• 2,140 points
9 views
0 votes
1 answer

Cannot access web3 object with typescript and ethereum

You still need to instantiate it first. ...READ MORE

answered Sep 25, 2018 in Blockchain by slayer
• 29,310 points
1,849 views
0 votes
1 answer

How to use moment.js library in angular 2 typescript app?

REFERENCE: https://momentjs.com/docs/#/use-it/typescript/ You can install this by using: npm install ...READ MORE

answered Jun 1 in TypeSript by Nina
• 3,020 points
52 views
0 votes
1 answer

how to use spread syntax and new Set() with TypeScript

The ... operator should work on anything ...READ MORE

answered Jun 10 in TypeSript by Nina
• 3,020 points
10 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP