DEV Community

Cover image for Creating a React Input component in Typescript
Gisela Miranda Difini
Gisela Miranda Difini

Posted on

Creating a React Input component in Typescript

Here is a nice way to create a React Input component using typescript!

Using an interface to declare your own prop types as well as extending all HTMLInputElement (https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement) properties to allow your component to accept all those input props you might need to increase accessibility for example πŸ˜„

Here you can see that by extending those HTMLInputElement properties in InputProps we can have all input element props passed to this component. Instead of having to declare each one we can just use a spread attribute ...rest

Input component auto complete

React input component with spread attribute

Original post: https://giselamirandadifini.com/creating-a-react-input-component-in-typescript

Top comments (9)

Collapse
Β 
leomjaques profile image
leo πŸ‘¨πŸ»β€πŸ’» β€’

Nice one! Thanks for posting it :)

Collapse
Β 
andrewbaisden profile image
Andrew Baisden β€’

Good article I really should start working with Typescript.

Collapse
Β 
karnikhonza profile image
Jan KΓ‘rnΓ­k β€’

Thanks a lot for this! I was stuck trying to pass the Input Props through another custom component to the input element. This has solved it for me! Great work!

Collapse
Β 
maxphillipsdev profile image
Max Phillips β€’

This saved me thanks :)

Collapse
Β 
enbonnet profile image
Ender Bonnet β€’

Thanks

Collapse
Β 
gvinod1991 profile image
Vinod Godti β€’

Good one

Collapse
Β 
ekimkael profile image
Ekim Kael β€’

Thanks so much

Collapse
Β 
wyrdolive profile image
Mary Olive β€’

Just what I was looking for! Thank you! :)

Collapse
Β 
dgcp3 profile image
DGCP3 β€’

why not React.ComponentProps<"input">