{"id":38,"date":"2016-09-27T08:50:32","date_gmt":"2016-09-27T08:50:32","guid":{"rendered":"http:\/\/iamakulov.com\/notes2\/?p=38"},"modified":"2017-05-09T22:09:02","modified_gmt":"2017-05-09T19:09:02","slug":"react-antipattern-dont-make-li-your-components-root","status":"publish","type":"post","link":"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/","title":{"rendered":"React anti-pattern: don\u2019t make &lt;li> your component\u2019s root"},"content":{"rendered":"<p>Noticed a React anti-pattern while mentoring a younger front-end developer on our project.<\/p>\n<p>Imagine you have a component that represents a list of articles. At one point, you realize that each article in the list is too complex:<\/p>\n<pre><code >const ArticleList = (props) =&gt; {\r\n  return &lt;div class=&quot;articles&quot;&gt;\r\n    &lt;ul class=&quot;article-list&quot;&gt;\r\n      { props.articles.map(article =&gt; \r\n        &lt;li class=&quot;article&quot;&gt;\r\n          &lt;h2 class=&quot;article__title&quot;&gt;{ article.title }&lt;\/h2&gt;\r\n          { \/* 25 other tags *\/ }\r\n        &lt;\/li&gt;\r\n      ) }\r\n    &lt;\/ul&gt;\r\n    { \/* ... *\/ }\r\n  &lt;\/div&gt;;\r\n}\r\n<\/code><\/pre>\n<p>So you decide to move the item to a separate component. You take that code inside <em>map()<\/em>, extract it into <em>&lt;Article><\/em> and get something like this:<\/p>\n<pre><code >const Article = (props) =&gt; {\r\n  return &lt;li class=&quot;article&quot;&gt;\r\n    &lt;h2 class=&quot;article__title&quot;&gt;{ props.title }&lt;\/h2&gt;\r\n    { \/* 25 other tags *\/ }\r\n  &lt;\/li&gt;;\r\n}\r\n\r\nconst ArticleList = (props) =&gt; {\r\n  return &lt;div class=&quot;articles&quot;&gt;\r\n    &lt;ul class=&quot;article-list&quot;&gt;\r\n      { props.articles.map(article =&gt; \r\n        &lt;Article title={article.title} { \/* other props *\/ } \/&gt;\r\n      ) }\r\n    &lt;\/ul&gt;\r\n    { \/* ... *\/ }\r\n  &lt;\/div&gt;;\r\n}\r\n<\/code><\/pre>\n<p><strong>Don\u2019t do it this way.<\/strong> This approach is wrong. The problem is that by taking a <em>&lt;li><\/em> and making it a root of the component, you\u2019ve just made your component <em>non-reusable<\/em>. This means that if you\u2019d like to reuse <em>&lt;Article><\/em> in another place, you\u2019ll only be able to apply it somewhere inside a list \u2013 because of this <em>&lt;li><\/em>. If you decide to render <em>&lt;Article><\/em> into e.g. a <em>&lt;div><\/em>, not only will this be non-semantic (<em>&lt;li><\/em> can only appear inside of <em>&lt;ul><\/em>s), but will also add unnecessary list item styling which is super weird.<\/p>\n<h1 id=\"solution\">Solution<a href=\"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/#solution\" class=\"heading-link\" aria-label=\"Link to this section\" title=\"Link to this section\">#<\/a><\/h1>\n<p>The solution is simple: move the <em>&lt;li><\/em> back into the <em>&lt;ArticleList><\/em> component and make the <em>&lt;Article><\/em>\u2019s root element a <em>&lt;div><\/em> or something else. This will probably require some refactoring in your styles, but will make the component reusable. Look how cool:<\/p>\n<pre><code >const Article = (props) =&gt; {\r\n  \/\/ Notice: &lt;li&gt;s are gone\r\n  return &lt;div class=&quot;article&quot;&gt;\r\n    &lt;h2 class=&quot;article__title&quot;&gt;{ props.title }&lt;\/h2&gt;\r\n    { \/* ... *\/ }\r\n  &lt;\/div&gt;;\r\n}\r\n\r\nconst ArticleList = (props) =&gt; {\r\n  return &lt;div class=&quot;articles&quot;&gt;\r\n    &lt;ul class=&quot;article-list&quot;&gt;\r\n      { props.articles.map(article =&gt; \r\n        &lt;li class=&quot;article-list__item&quot;&gt;\r\n          &lt;Article title={article.title} { \/* other props *\/ } \/&gt;\r\n        &lt;\/li&gt;\r\n      ) }\r\n    &lt;\/ul&gt;\r\n    { \/* ... *\/ }\r\n  &lt;\/div&gt;;\r\n}\r\n\r\n\/\/ And now you can easily render an article inside of a sidebar \u2013 have no idea why though\r\nconst Sidebar = (props) =&gt; {\r\n  return &lt;div class=&quot;sidebar&quot;&gt;\r\n    &lt;div class=&quot;sidebar__article&quot;&gt;\r\n      &lt;Article title=&quot;Look ma&quot; { \/* other props *\/ } \/&gt;\r\n    &lt;\/div&gt;\r\n    { \/* ... *\/ }\r\n  &lt;\/div&gt;;\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Noticed a React anti-pattern while mentoring a younger front-end developer on our project. Imagine you have a component that represents a list of articles. At one point, you realize that each article in the list is too complex: So you decide to move the item to a separate component. You take that code inside map(), &hellip; <a href=\"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;React anti-pattern: don\u2019t make &lt;li> your component\u2019s root&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","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>React anti-pattern: don\u2019t make &lt;li&gt; your component\u2019s root - 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\/react-antipattern-dont-make-li-your-components-root\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React anti-pattern: don\u2019t make &lt;li&gt; your component\u2019s root - Ivan Akulov\u2019s blog\" \/>\n<meta property=\"og:description\" content=\"Noticed a React anti-pattern while mentoring a younger front-end developer on our project. Imagine you have a component that represents a list of articles. At one point, you realize that each article in the list is too complex: So you decide to move the item to a separate component. You take that code inside map(), &hellip; Continue reading &quot;React anti-pattern: don\u2019t make &lt;li&gt; your component\u2019s root&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/\" \/>\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=\"2016-09-27T08:50:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-05-09T19:09:02+00:00\" \/>\n<meta name=\"author\" content=\"Ivan Akulov\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\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\/react-antipattern-dont-make-li-your-components-root\/\",\"url\":\"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/\",\"name\":\"React anti-pattern: don\u2019t make &lt;li> your component\u2019s root - Ivan Akulov\u2019s blog\",\"isPartOf\":{\"@id\":\"https:\/\/iamakulov.com\/notes\/#website\"},\"datePublished\":\"2016-09-27T08:50:32+00:00\",\"dateModified\":\"2017-05-09T19:09:02+00:00\",\"author\":{\"@id\":\"https:\/\/iamakulov.com\/notes\/#\/schema\/person\/ebf7b61bf573e7be5fe438f50ebd9b81\"},\"breadcrumb\":{\"@id\":\"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/iamakulov.com\/notes\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React anti-pattern: don\u2019t make &lt;li> your component\u2019s root\"}]},{\"@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":"React anti-pattern: don\u2019t make &lt;li> your component\u2019s root - 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\/react-antipattern-dont-make-li-your-components-root\/","og_locale":"en_US","og_type":"article","og_title":"React anti-pattern: don\u2019t make &lt;li> your component\u2019s root - Ivan Akulov\u2019s blog","og_description":"Noticed a React anti-pattern while mentoring a younger front-end developer on our project. Imagine you have a component that represents a list of articles. At one point, you realize that each article in the list is too complex: So you decide to move the item to a separate component. You take that code inside map(), &hellip; Continue reading \"React anti-pattern: don\u2019t make &lt;li> your component\u2019s root\"","og_url":"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/","og_site_name":"Ivan Akulov\u2019s blog","article_publisher":"http:\/\/facebook.com\/iamakulov.page","article_published_time":"2016-09-27T08:50:32+00:00","article_modified_time":"2017-05-09T19:09:02+00:00","author":"Ivan Akulov","twitter_card":"summary_large_image","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\/react-antipattern-dont-make-li-your-components-root\/","url":"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/","name":"React anti-pattern: don\u2019t make &lt;li> your component\u2019s root - Ivan Akulov\u2019s blog","isPartOf":{"@id":"https:\/\/iamakulov.com\/notes\/#website"},"datePublished":"2016-09-27T08:50:32+00:00","dateModified":"2017-05-09T19:09:02+00:00","author":{"@id":"https:\/\/iamakulov.com\/notes\/#\/schema\/person\/ebf7b61bf573e7be5fe438f50ebd9b81"},"breadcrumb":{"@id":"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/iamakulov.com\/notes\/react-antipattern-dont-make-li-your-components-root\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/iamakulov.com\/notes\/"},{"@type":"ListItem","position":2,"name":"React anti-pattern: don\u2019t make &lt;li> your component\u2019s root"}]},{"@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\/38"}],"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=38"}],"version-history":[{"count":4,"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":42,"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/posts\/38\/revisions\/42"}],"wp:attachment":[{"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iamakulov.com\/notes\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}