{"id":692,"date":"2017-09-13T09:45:50","date_gmt":"2017-09-13T06:45:50","guid":{"rendered":"https:\/\/iamakulov.com\/notes\/?p=692"},"modified":"2017-11-08T17:47:31","modified_gmt":"2017-11-08T14:47:31","slug":"deep-proptypes","status":"publish","type":"post","link":"https:\/\/iamakulov.com\/notes\/deep-proptypes\/","title":{"rendered":"Describe your React propTypes as deeply as possible"},"content":{"rendered":"<p>On my project, I\u2019ve been noticing code like this:<\/p>\n<pre><code class=\"javascript\">class Header extends React.Component {\r\n  static propTypes = {\r\n    items: PropTypes.array,\r\n    counters: PropTypes.object,\r\n  };\r\n  \r\n  \/\/ ...\r\n}\r\n<\/code><\/pre>\n<p>This is how it should look like instead:<\/p>\n<pre><code class=\"javascript\">class Header extends React.Component {\r\n  static propTypes = {\r\n    items: PropTypes.arrayOf(PropTypes.shape({\r\n      id: PropTypes.string.isRequired,\r\n      name: PropTypes.string.isRequired,\r\n      link: PropTypes.string.isRequired,\r\n    })).isRequired,\r\n    counters: PropTypes.objectOf(PropTypes.number).isRequired,\r\n  };\r\n\r\n  \/\/ ...\r\n}\r\n<\/code><\/pre>\n<p>The difference is that in the latter component, propTypes are much more detailed. It\u2019s better for two reasons:<\/p>\n<ul>\n<li><strong>React validates your props better.<\/strong> In the former component, you won\u2019t get any warnings if you pass a wrong array into <code>items<\/code> or if you forget to pass it at all. Instead, you\u2019ll have a wrong rendering result or a runtime error and will have to debug it.<\/li>\n<li>\n<p><strong>You understand the interface of the component easier.<\/strong> This is even more important.<\/p>\n<p>With the latter component, to understand the structure of <code>items<\/code>, you just look at its propTypes. With the former one, you have to dive into its code. It\u2019s not a problem when the component has been created just 10 minutes before, and you remember what it accepts, but it makes further support <em>way<\/em> easier.<\/p>\n<\/li>\n<\/ul>\n<p>There\u2019s only one case when I find it acceptable to skip some propTypes definitions. It\u2019s when your component just passes a prop to a child component and doesn\u2019t care about its structure. In this case, the child component should validate the prop:<\/p>\n<div class='annotated-element annotated-element_has-code_no annotated-element_keep-size_no annotated-element_mobile-direction_content-then-annotation'>\n<div class='annotated-element__annotation'>Note how the <code>items<\/code><br \/>\npropType in <code>Header<\/code> cares only about the <code>id<\/code> field it uses, and <code>counters<\/code> doesn\u2019t care about its items type at all.<\/div>\n<div class='annotated-element__content'>\n<pre><code class=\"javascript\">class Header extends React.Component {\r\n  static propTypes = {\r\n    items: PropTypes.arrayOf(PropTypes.shape({\r\n      id: PropTypes.string.isRequired,\r\n    })).isRequired,\r\n    counters: PropTypes.objectOf(PropTypes.any).isRequired,\r\n  };\r\n\r\n  render() {\r\n    return &lt;div>\r\n      {this.props.items.map(item =>\r\n        &lt;HeaderItem item={item} counter={this.props.counters[item.id]} \/>\r\n      }\r\n    &lt;\/div>;\r\n  }\r\n}\r\n\r\nclass HeaderItem extends React.Component {\r\n  static propTypes = {\r\n    item: PropTypes.shape({\r\n      name: PropTypes.string.isRequired,\r\n      link: PropTypes.string.isRequired,\r\n    }).isRequired,\r\n    counter: PropTypes.number.isRequired,\r\n  };\r\n\r\n  \/\/ ...\r\n}\r\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p>Here\u2019s the rule:<\/p>\n<div class='large-text-left'>Describe your propTypes as deeply as possible<\/div>\n","protected":false},"excerpt":{"rendered":"<p>On my project, I\u2019ve been noticing code like this: This is how it should look like instead: The difference is that in the latter component, propTypes are much more detailed. It\u2019s better for two reasons: React validates your props better. In the former component, you won\u2019t get any warnings if you pass a wrong array &hellip; <a href=\"https:\/\/iamakulov.com\/notes\/deep-proptypes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Describe your React propTypes as deeply as possible&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[6,7],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Describe your React propTypes as deeply as possible - Ivan Akulov\u2019s blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/iamakulov.com\/notes\/deep-proptypes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Describe your React propTypes as deeply as possible\" \/>\n<meta property=\"og:description\" content=\"Why just \u201cPropTypes.array\u201d is not acceptable\" \/>\n<meta property=\"og:url\" content=\"https:\/\/iamakulov.com\/notes\/deep-proptypes\/\" \/>\n<meta property=\"og:site_name\" content=\"Ivan Akulov\u2019s blog\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/iamakulov.page\" \/>\n<meta property=\"article:published_time\" content=\"2017-09-13T06:45:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-08T14:47:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/iamakulov.com\/notes2\/wp-content\/uploads\/2017\/09\/amplifr-emoji-composer-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ivan Akulov\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Describe your React propTypes as deeply as possible\" \/>\n<meta name=\"twitter:description\" content=\"Why just \u201cPropTypes.array\u201d is not acceptable\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/iamakulov.com\/notes2\/wp-content\/uploads\/2017\/09\/amplifr-emoji-composer-1.png\" \/>\n<meta name=\"twitter:creator\" content=\"@iamakulov\" \/>\n<meta name=\"twitter:site\" content=\"@iamakulov\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ivan Akulov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/iamakulov.com\/notes\/deep-proptypes\/\",\"url\":\"https:\/\/iamakulov.com\/notes\/deep-proptypes\/\",\"name\":\"Describe your React propTypes as deeply as possible - Ivan Akulov\u2019s blog\",\"isPartOf\":{\"@id\":\"https:\/\/iamakulov.com\/notes\/#website\"},\"datePublished\":\"2017-09-13T06:45:50+00:00\",\"dateModified\":\"2017-11-08T14:47:31+00:00\",\"author\":{\"@id\":\"https:\/\/iamakulov.com\/notes\/#\/schema\/person\/ebf7b61bf573e7be5fe438f50ebd9b81\"},\"breadcrumb\":{\"@id\":\"https:\/\/iamakulov.com\/notes\/deep-proptypes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/iamakulov.com\/notes\/deep-proptypes\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/iamakulov.com\/notes\/deep-proptypes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/iamakulov.com\/notes\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Describe your React propTypes as deeply as possible\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/iamakulov.com\/notes\/#website\",\"url\":\"https:\/\/iamakulov.com\/notes\/\",\"name\":\"Ivan Akulov\u2019s blog\",\"description\":\"Ivan Akulov writes about his front-end experience, React, webpack, and performance optimizations.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/iamakulov.com\/notes\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/iamakulov.com\/notes\/#\/schema\/person\/ebf7b61bf573e7be5fe438f50ebd9b81\",\"name\":\"Ivan Akulov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/iamakulov.com\/notes\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f68e4cd477cef1577c339e6f09736d3a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f68e4cd477cef1577c339e6f09736d3a?s=96&d=mm&r=g\",\"caption\":\"Ivan Akulov\"},\"description\":\"I'm a software engineer specializing in web performance, JavaScript, and React. I\u2019m also a Google Developer Expert. I work at Framer.\",\"sameAs\":[\"http:\/\/iamakulov.com\",\"https:\/\/x.com\/iamakulov\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Describe your React propTypes as deeply as possible - Ivan Akulov\u2019s blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/iamakulov.com\/notes\/deep-proptypes\/","og_locale":"en_US","og_type":"article","og_title":"Describe your React propTypes as deeply as possible","og_description":"Why just \u201cPropTypes.array\u201d is not acceptable","og_url":"https:\/\/iamakulov.com\/notes\/deep-proptypes\/","og_site_name":"Ivan Akulov\u2019s blog","article_publisher":"http:\/\/facebook.com\/iamakulov.page","article_published_time":"2017-09-13T06:45:50+00:00","article_modified_time":"2017-11-08T14:47:31+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/iamakulov.com\/notes2\/wp-content\/uploads\/2017\/09\/amplifr-emoji-composer-1.png","type":"image\/png"}],"author":"Ivan Akulov","twitter_card":"summary_large_image","twitter_title":"Describe your React propTypes as deeply as possible","twitter_description":"Why just \u201cPropTypes.array\u201d is not acceptable","twitter_image":"https:\/\/iamakulov.com\/notes2\/wp-content\/uploads\/2017\/09\/amplifr-emoji-composer-1.png","twitter_creator":"@iamakulov","twitter_site":"@iamakulov","twitter_misc":{"Written by":"Ivan Akulov","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/iamakulov.com\/notes\/deep-proptypes\/","url":"https:\/\/iamakulov.com\/notes\/deep-proptypes\/","name":"Describe your React propTypes as deeply as possible - Ivan Akulov\u2019s blog","isPartOf":{"@id":"https:\/\/iamakulov.com\/notes\/#website"},"datePublished":"2017-09-13T06:45:50+00:00","dateModified":"2017-11-08T14:47:31+00:00","author":{"@id":"https:\/\/iamakulov.com\/notes\/#\/schema\/person\/ebf7b61bf573e7be5fe438f50ebd9b81"},"breadcrumb":{"@id":"https:\/\/iamakulov.com\/notes\/deep-proptypes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/iamakulov.com\/notes\/deep-proptypes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/iamakulov.com\/notes\/deep-proptypes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/iamakulov.com\/notes\/"},{"@type":"ListItem","position":2,"name":"Describe your React propTypes as deeply as possible"}]},{"@type":"WebSite","@id":"https:\/\/iamakulov.com\/notes\/#website","url":"https:\/\/iamakulov.com\/notes\/","name":"Ivan Akulov\u2019s blog","description":"Ivan Akulov writes about his front-end experience, React, webpack, and performance optimizations.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/iamakulov.com\/notes\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/iamakulov.com\/notes\/#\/schema\/person\/ebf7b61bf573e7be5fe438f50ebd9b81","name":"Ivan Akulov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/iamakulov.com\/notes\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f68e4cd477cef1577c339e6f09736d3a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f68e4cd477cef1577c339e6f09736d3a?s=96&d=mm&r=g","caption":"Ivan Akulov"},"description":"I'm a software engineer specializing in web performance, JavaScript, and React. I\u2019m also a Google Developer Expert. I work at Framer.","sameAs":["http:\/\/iamakulov.com","https:\/\/x.com\/iamakulov"]}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/posts\/692"}],"collection":[{"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/comments?post=692"}],"version-history":[{"count":15,"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/posts\/692\/revisions"}],"predecessor-version":[{"id":837,"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/posts\/692\/revisions\/837"}],"wp:attachment":[{"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/media?parent=692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/categories?post=692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/tags?post=692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}