Skip to content
Advertisement

How to create a reusable Input component with React & TypeScript?

It’s my first attempt with React TypeScript and don’t fully understand the error I’m getting when trying to build a reusable Input component. What is the TypeScript way of approaching this kind of component?

See Component & Error below:

JavaScript

Error

JavaScript

Advertisement

Answer

I don’t think FC is satisfying the type constraint here, try ForwardRefRenderFunction instead:

JavaScript

Alternatively, you can combine Input and FormInput into one and let TypeScript infer for you:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement