Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting anonymous function components React Typescript

I've just recently started using Typescript and I haven't found an easy way to achieve this

import React from 'react'
...
export default () => <div>My awesome anonymous functional component</div>;

I normally use this within an index.jsx / index.tsx file so the syntax helps my component stay clean.

like image 980
alanionita Avatar asked Sep 12 '26 08:09

alanionita


1 Answers

You should still be able to do that without issues, but the syntax I prefer is the following:

export const MyComponent: React.FC = () => <div>My component</div>

I tend to avoid using default exports in typescript for some of the reasons mentioned here: https://basarat.gitbook.io/typescript/main-1/defaultisbad

Also, with the above, I'll get the necessary typings for a functional component such as children, etc.

like image 95
Alex K Avatar answered Sep 14 '26 20:09

Alex K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!