{"id":2010,"date":"2018-05-28T19:51:24","date_gmt":"2018-05-29T00:51:24","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2010"},"modified":"2018-05-28T19:51:24","modified_gmt":"2018-05-29T00:51:24","slug":"react-component-render-can-return-one-element","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/05\/28\/react-component-render-can-return-one-element\/","title":{"rendered":"React: A component render can return only one element!"},"content":{"rendered":"<p>A component render can return only one element, so if you want more complicated HTML output from your component&#8217;s render, it needs to be returned in a single div. In other words, every component can return only a single div.<\/p>\n<p>This applies to ReactDOM.render also. So if I want to render a Movie component three times, I need to package the three movie elements into a single div. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">ReactDOM.render(\r\n  &lt;div&gt;\r\n    &lt;Movie title=\"Avatar\" genre=\"action\"\/&gt;\r\n    &lt;Movie title=\"Pee Wees Big Adventure\" genre=\"adventure\"\/&gt;\r\n    &lt;Movie title=\"Star Wars\" genre=\"sci-fi\"\/&gt;\r\n  &lt;\/div&gt;,\r\ndocument.getElementById('app')\r\n);<\/pre>\n<p>If you look at the render portion of every component in my React tic-tac-toe game, you will see that all of the HTML output, no matter how much is there, is always returned in a single div.<\/p>\n<p>For complete React code that demonstrates this in action, see my tic-tac-toe react script here:<br \/>\n<a href=\"https:\/\/github.com\/chris-relaxing\/react-tic-tac-toe\/blob\/master\/tic-tac-toe.js\">https:\/\/github.com\/chris-relaxing\/react-tic-tac-toe\/blob\/master\/tic-tac-toe.js<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A component render can return only one element, so if you want more complicated HTML output from your component&#8217;s render, it needs to be returned in a single div. In other words, every component can return only a single div. This applies to ReactDOM.render also. So if I want to render a Movie component three &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/05\/28\/react-component-render-can-return-one-element\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">React: A component render can return only one element!<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,111],"tags":[45,110],"class_list":["post-2010","post","type-post","status-publish","format-standard","hentry","category-javascript-language","category-react-js","tag-javascript","tag-react-js"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2010"}],"version-history":[{"count":3,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2010\/revisions"}],"predecessor-version":[{"id":2013,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2010\/revisions\/2013"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}