<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: José Pereira</title>
    <description>The latest articles on DEV Community by José Pereira (@jomifepe).</description>
    <link>https://dev.to/jomifepe</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F466880%2F7fc95d48-7201-4f69-aee6-787396e0edd0.JPG</url>
      <title>DEV Community: José Pereira</title>
      <link>https://dev.to/jomifepe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jomifepe"/>
    <language>en</language>
    <item>
      <title>Clean up your React component types 🧼</title>
      <dc:creator>José Pereira</dc:creator>
      <pubDate>Tue, 04 Jan 2022 14:16:24 +0000</pubDate>
      <link>https://dev.to/jomifepe/clean-up-your-react-component-types-2502</link>
      <guid>https://dev.to/jomifepe/clean-up-your-react-component-types-2502</guid>
      <description>&lt;p&gt;In this article, we go into some practices you can follow in your React codebase with Function Components and TypeScript to cleanly type your components, and hopefully write less code that serves the same purpose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ℹ️ Let me start by saying that this article is opinionated and it’s not trying to define what are the correct practices.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  TL;DR
&lt;/h1&gt;

&lt;p&gt;Here’s a quick summary of this article, if you’re in a hurry:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can take advantage of TypeScript’s type inference to avoid explicitly defining component types, which means less and cleaner code.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;React.FC&lt;/code&gt; should not be used when defining components on codebases with Function Components and TypeScript, because it:

&lt;ul&gt;
&lt;li&gt;Has unnecessary (when using TypeScript) and legacy properties, like &lt;code&gt;propTypes&lt;/code&gt;, &lt;code&gt;contextTypes&lt;/code&gt;, and &lt;code&gt;defaultProps&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Limits you to use function expressions.&lt;/li&gt;
&lt;li&gt;Doesn’t support generics.&lt;/li&gt;
&lt;li&gt;Complicates the types of Components with a Namespace.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Cleaner component types make your code more future-proof and decoupled.&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  1. Let TypeScript do the work 💪
&lt;/h1&gt;

&lt;p&gt;You might’ve heard that TypeScript offers &lt;a href="https://www.typescriptlang.org/docs/handbook/type-inference.html" rel="noopener noreferrer"&gt;&lt;strong&gt;type inference&lt;/strong&gt;&lt;/a&gt;. This means that we don’t need to explicitly define the type of a variable, as long as it can be inferred from its value. It’s a simple concept that can help clean your codebase of unnecessary explicit types.&lt;/p&gt;

&lt;p&gt;Applied to React components, it’s arguably cleaner to let TypeScript infer the component’s return type than it is to explicitly define it, as portrayed by the following snippet.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You might argue that explicitly defining your return type is safer, which is true, so you should always be a little more careful. When using type inference, you should &lt;strong&gt;always check the inferred type to avoid making mistakes&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Tip — Enable or disable the following eslint rule to be forced to add an explicit return type or not: &lt;code&gt;typescript-eslint/explicit-function-return-type&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;A brief history recap before the next topic. From 2013 to 2019, React &lt;strong&gt;components were written using classes&lt;/strong&gt;, that would extend other built-in ones, like &lt;code&gt;React.Component&lt;/code&gt; and &lt;code&gt;React.PureComponent&lt;/code&gt;, to make up a valid component. But, since the release of hooks, in v16.8, &lt;strong&gt;we’ve transitioned to write components as functions&lt;/strong&gt;, which is much easier, because you write less, and everything is still there.&lt;/p&gt;

&lt;p&gt;Function Components are rather &lt;strong&gt;simple&lt;/strong&gt;, as they don’t require anything to be considered a component, other than returning valid component values, like &lt;a href="https://reactjs.org/docs/introducing-jsx.html" rel="noopener noreferrer"&gt;JSX&lt;/a&gt;. Although, quite a few people still feel the need to mark their functions as components. This is frequently done by using the &lt;code&gt;React.FC&lt;/code&gt;type from the &lt;code&gt;@types/react&lt;/code&gt; package. The thing about this type is that it brings &lt;strong&gt;old and unnecessary properties&lt;/strong&gt;, some of them from class-based React, to your otherwise clean function components. How do we solve this?&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Stop using React.FC
&lt;/h1&gt;

&lt;p&gt;Types like React’s &lt;code&gt;FunctionComponent&lt;/code&gt;/&lt;code&gt;FC&lt;/code&gt; and &lt;code&gt;VoidFunctionComponent&lt;/code&gt;/&lt;code&gt;VFC&lt;/code&gt; were introduced to &lt;strong&gt;ease the creation of function components&lt;/strong&gt;, while still having some &lt;strong&gt;useful properties&lt;/strong&gt;, like &lt;code&gt;children&lt;/code&gt;, and other internal ones, like &lt;code&gt;propTypes&lt;/code&gt;, &lt;code&gt;contextTypes&lt;/code&gt;, &lt;code&gt;defaultProps&lt;/code&gt;, and &lt;code&gt;displayName&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1.1. But do we need all these properties? 🤔
&lt;/h2&gt;

&lt;p&gt;Well, to answer that, let’s dissect the &lt;code&gt;FC&lt;/code&gt; type for example, which is &lt;a href="https://github.com/search?l=TSX&amp;amp;q=React.FC&amp;amp;type=Code" rel="noopener noreferrer"&gt;undoubtedly the most used one&lt;/a&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;1️⃣ &lt;code&gt;(props: PropsWithChildren&amp;lt;P&amp;gt;, context?: any): ReactElement&amp;lt;any, any&amp;gt; | null&lt;/code&gt; — This first property defines the parameters and return type of &lt;strong&gt;the function component itself&lt;/strong&gt;. Let’s take a closer look at it:

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;props&lt;/code&gt; parameter on the function corresponds to the properties that it accepts (defined by the user) &lt;strong&gt;plus an implicit &lt;code&gt;children&lt;/code&gt; property&lt;/strong&gt;. This is misleading, as a lot of components don’t accept &lt;strong&gt;children&lt;/strong&gt;. If you’re not doing 
anything with that property, as of &lt;a href="https://github.com/DefinitelyTyped/DefinitelyTyped/pull/46643" rel="noopener noreferrer"&gt;&lt;code&gt;@types/react v16.9.48&lt;/code&gt;&lt;/a&gt;, you can use the &lt;code&gt;VoidFunctionComponent&lt;/code&gt;/&lt;code&gt;VFC&lt;/code&gt; type instead, which does the same, but doesn’t add it to your type.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;context&lt;/code&gt; parameter is used to pass &lt;a href="https://reactjs.org/docs/context.html" rel="noopener noreferrer"&gt;&lt;strong&gt;context&lt;/strong&gt;&lt;/a&gt; to descendant components. When using hooks, this property is unnecessary, as we can and should resort to the &lt;code&gt;useContext&lt;/code&gt; hook to consume context.&lt;/li&gt;
&lt;li&gt;The type of this property (right side of the colon) defines that the component can only return a &lt;code&gt;ReactElement&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;, preventing it from returning invalid values, like &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;2️⃣ &lt;a href="https://reactjs.org/docs/typechecking-with-proptypes.html" rel="noopener noreferrer"&gt;&lt;code&gt;propTypes&lt;/code&gt;&lt;/a&gt; — Allows the &lt;strong&gt;assignment of data types&lt;/strong&gt; to the component’s properties, to be able to add type checking to JavaScript components. As you might expect, this isn’t useful when using TypeScript, as we can already define our types, which it also accepts as a parameter: &lt;code&gt;FC&amp;lt;MyType&amp;gt;&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;3️⃣ &lt;a href="https://reactjs.org/docs/typechecking-with-proptypes.html#default-prop-values" rel="noopener noreferrer"&gt;&lt;code&gt;contextTypes&lt;/code&gt;&lt;/a&gt; — Has the same function as &lt;code&gt;propTypes&lt;/code&gt;, but applied to the &lt;a href="https://reactjs.org/docs/context.html" rel="noopener noreferrer"&gt;&lt;code&gt;context&lt;/code&gt;&lt;/a&gt; passed to the component (mentioned above on the first property). This was used in JavaScript classes with legacy context.&lt;/li&gt;

&lt;li&gt;4️⃣ &lt;a href="https://reactjs.org/docs/typechecking-with-proptypes.html#default-prop-values" rel="noopener noreferrer"&gt;&lt;code&gt;defaultProps&lt;/code&gt;&lt;/a&gt; — Allows the assignment of a &lt;strong&gt;default value&lt;/strong&gt; to the properties accepted by the component. Useful for classes, but when using function components, we’re able to use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters" rel="noopener noreferrer"&gt;ES6’s default parameters&lt;/a&gt; instead:

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;/li&gt;

&lt;li&gt;5️⃣ &lt;a href="https://reactjs.org/docs/react-component.html#displayname" rel="noopener noreferrer"&gt;&lt;code&gt;displayName&lt;/code&gt;&lt;/a&gt; — Allows the user to assign an explicit name to a component, to be used in &lt;strong&gt;debugging&lt;/strong&gt; messages. By default, the name is inferred from the name or the function or class that defined the component.&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  1.2. Limitations of React.FC 👎
&lt;/h2&gt;

&lt;p&gt;What was mentioned above is not very disruptive, but rather a matter of using cleaner types. Although, there are other more limiting downsides of the &lt;code&gt;FC&lt;/code&gt; type.&lt;/p&gt;

&lt;h3&gt;
  
  
  A. Forces you to type the function, not the props
&lt;/h3&gt;

&lt;p&gt;When using &lt;code&gt;FC&lt;/code&gt;, you need to type the function, not its props, which forces you to use function expressions, as function declarations cannot be typed.&lt;/p&gt;

&lt;p&gt;Here’s what I mean:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This limitation prevents you, for example, from freely positioning your functions, as &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function#function_expression_hoisting" rel="noopener noreferrer"&gt;function expressions are not hoisted&lt;/a&gt;, and cannot be used before their definition.&lt;/p&gt;

&lt;h3&gt;
  
  
  B. Doesn’t support generics
&lt;/h3&gt;

&lt;p&gt;If you want to write a function component with generic types, &lt;code&gt;FC&lt;/code&gt; won’t work for you, as there’s no valid syntax to do so.&lt;br&gt;
For example, let’s consider the following component:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;When using &lt;code&gt;FC&lt;/code&gt;, where do we define the generic?&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This causes inconsistencies in your component definitions, as some of them will be using &lt;code&gt;React.FC,&lt;/code&gt; and the ones that accept generic types, will not.&lt;/p&gt;

&lt;h3&gt;
  
  
  C. Requires more code to create a Component with a Namespace
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://medium.com/@kunukn_95852/react-components-with-namespace-f3d169feaf91" rel="noopener noreferrer"&gt;This “pattern”&lt;/a&gt; is commonly used in React, and it’s fairly easy to implement using regular functions.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;But writing the code above with &lt;code&gt;FC&lt;/code&gt; requires you to explicitly define all your types on the &lt;code&gt;namespace component&lt;/code&gt;, which increases the boilerplate.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This is not a major downside, but rather a small sacrifice in simplicity.&lt;/p&gt;

&lt;h2&gt;
  
  
  1.3. What are the alternatives to React.FC?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;FC&lt;/code&gt; type adds everything you need for a component to work, which is one of the main reasons it’s widely used. But it’s easy to achieve the same results with cleaner types, by writing some utility types for yourself.&lt;/p&gt;

&lt;p&gt;The following are some alternatives that allow you have some of the properties offered by &lt;code&gt;FC&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  A. children
&lt;/h3&gt;

&lt;p&gt;If our component accepts &lt;code&gt;children&lt;/code&gt;, there are multiple ways to type it for such:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;1️⃣ Using helper types from@types/react&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This package allows you to add this property to your custom type by using the PropsWithChildren utility.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The only issue with this type is that it requires an argument, so if you have a component that only needs &lt;code&gt;children&lt;/code&gt; as it’s props, it doesn’t allow you to do the following: &lt;code&gt;(props: PropsWithChildren)&lt;/code&gt; ❌&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;2️⃣ Defining the property in your type&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There’s always the possibility of defining the property in your custom type.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;3️⃣ Define your own utility type&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don’t want to type it every time? That’s okay, I’m lazy too.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  B. displayName
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;displayName&lt;/code&gt; might also be useful for debugging, so if you wish to override it, do it just like you normally would.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;No need to define the property in your component type, as TypeScript will infer it 🚀&lt;/p&gt;

&lt;h3&gt;
  
  
  C. What about the other React.FC properties?
&lt;/h3&gt;

&lt;p&gt;I’m confident that you don’t need them. Nowadays, if you’re using Function Components with TypeScript on your codebase, unless you’re doing some workaround, you won’t need properties like &lt;code&gt;propTypes&lt;/code&gt;, &lt;code&gt;contextTypes&lt;/code&gt;, or &lt;code&gt;defaultProps&lt;/code&gt;. If you do, feel free to leave a comment with your use case, I’m always open to learning and discussing this topic.&lt;/p&gt;

&lt;h2&gt;
  
  
  1.5. Is there any benefit to using React.FC?
&lt;/h2&gt;

&lt;p&gt;Types such as &lt;code&gt;FunctionComponent&lt;/code&gt;/&lt;code&gt;FC&lt;/code&gt; and &lt;code&gt;VoidFunctionComponent&lt;/code&gt;/&lt;code&gt;VFC&lt;/code&gt; have nothing wrong on their own. If you’re indifferent to their limitations, they can be great in the following scenarios.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Beginners getting into typed React&lt;/li&gt;
&lt;li&gt;JavaScript codebases&lt;/li&gt;
&lt;li&gt;Legacy codebases that use class-based components or legacy context code&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  Why does this matter?
&lt;/h1&gt;

&lt;p&gt;You might argue that improving your component types is irrelevant because, in the end, everything is transpiled to JavaScript and the code won’t run faster because of it. While that’s true, I believe these small changes have a couple of benefits.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🚀 Increases &lt;strong&gt;developer experience&lt;/strong&gt; and code &lt;strong&gt;readability&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;🧼 Promotes &lt;strong&gt;clean code practices&lt;/strong&gt; and the use of built-in JavaScript &amp;amp; TypeScript functionalities.&lt;/li&gt;
&lt;li&gt;💪 Strengthens developer knowledge of TypeScript and React’s internal mechanisms. Quite a few people are unaware of the reasons behind using &lt;code&gt;React.FC&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;🧩 Makes your code more &lt;strong&gt;future-proof and decoupled&lt;/strong&gt;. This means that if, for any reason, a built-in type changes, you have less chance of being affected by it. Also, if you wish to move to another React-based alternative (like &lt;strong&gt;preact&lt;/strong&gt;), the process is easier, as you are not coupled to the &lt;code&gt;@types/react&lt;/code&gt; package (&lt;a href="https://fettblog.eu/typescript-react-why-i-dont-use-react-fc/#3.-easier-to-move-to-preact" rel="noopener noreferrer"&gt;more info about this in this great article&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nonetheless, this is not just my personal opinion, as back in early 2020, &lt;code&gt;React.FC&lt;/code&gt; &lt;a href="https://github.com/facebook/create-react-app/pull/8177" rel="noopener noreferrer"&gt;was removed from the official create-react-app&lt;/a&gt;, for some of the reasons mentioned above.&lt;/p&gt;

&lt;p&gt;If you wish to remove all instances of &lt;code&gt;React.FC&lt;/code&gt; from your codebase, you can use &lt;a href="https://github.com/gndelia/codemod-replace-react-fc-typescript" rel="noopener noreferrer"&gt;this jscodeshift codemod&lt;/a&gt; (Tip: Use &lt;code&gt;npx&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;📚 There are also &lt;strong&gt;great resources and articles&lt;/strong&gt; discussing this, from which I based myself on. Be sure to give them a read:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://fettblog.eu/typescript-react-why-i-dont-use-react-fc/" rel="noopener noreferrer"&gt;TypeScript + React: Why I don’t use React.FC&lt;/a&gt; by Stefan Baumgartner&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.harrymt.com/blog/2020/05/20/react-typescript-react-fc.html" rel="noopener noreferrer"&gt;Should you use React.FC for typing React Components&lt;/a&gt; by Harry Mumford-Turner&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://medium.com/raccoons-group/why-you-probably-shouldnt-use-react-fc-to-type-your-react-components-37ca1243dd13" rel="noopener noreferrer"&gt;Why you probably shouldn’t use React.FC to type your React components&lt;/a&gt; by Sam Hendrickx&lt;/li&gt;
&lt;li&gt;Not an article, but a good cheatsheet with some great tips: &lt;a href="https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/function_components/" rel="noopener noreferrer"&gt;Function Components | React TypeScript Cheatsheets&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thank you for reading, I hope you learned something from this article, I know I did 🚀&lt;/p&gt;

&lt;p&gt;You can also find this article on &lt;a href="https://medium.com/xgeeks/clean-up-your-react-component-types-45acec85d4c3" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
