Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import Event Types from React (TypeScript)

How to import Event Types from React?

For an example how to import KeyboardEvent so it can be used for onKeyDown callback type annotation?

I have browsed Material-ui .d.ts file and found that in that file EventTypes are used as defined in React

onKeyDown?: React.KeyboardEventHandler;

And in React .d.ts

type KeyboardEventHandler = EventHandler<KeyboardEvent>;

But I can't find a way to use import them ...

like image 304
Иво Стратев Avatar asked Jul 12 '16 19:07

Иво Стратев


People also ask

How do I import an event into React?

Add KeyboardEvent to your react import statement. Then, in your handler you can use the KeyboardEvent type. If you don't specify it in the import statement, you will use ES5 declaration of KeyBoardEvent, which won't compile with your react project. Save this answer.


1 Answers

It's works fine for me:

handleKeyPress (event: React.KeyboardEvent): any {
}

And I use material-ui too

like image 119
Ser Avatar answered Sep 19 '22 22:09

Ser