{"id":4209,"date":"2023-05-14T10:26:07","date_gmt":"2023-05-14T10:26:07","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=4209"},"modified":"2023-06-10T11:29:10","modified_gmt":"2023-06-10T11:29:10","slug":"react-react-render-html","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/react-react-render-html\/","title":{"rendered":"React &#8211; React Render HTML"},"content":{"rendered":"<p class=\"intro\">React&#8217;s goal is in many ways to render HTML in a web page.<\/p>\n<p class=\"intro\">React renders HTML to the web page by using a function called\u00a0<code class=\"w3-codespan\">createRoot()<\/code>\u00a0and its method\u00a0<code class=\"w3-codespan\">render()<\/code>.<\/p>\n<hr \/>\n<h2>The createRoot Function<\/h2>\n<p>The\u00a0<code class=\"w3-codespan\">createRoot()<\/code>\u00a0function takes one argument, an HTML element.<\/p>\n<p>The purpose of the function is to define the HTML element where a React component should be displayed.<\/p>\n<h2>The render Method<\/h2>\n<p>The\u00a0<code class=\"w3-codespan\">render()<\/code>\u00a0method is then called to define the React component that should be rendered.<\/p>\n<p>But render where?<\/p>\n<p>There is another folder in the root directory of your React project, named &#8220;public&#8221;. In this folder, there is an\u00a0<code class=\"w3-codespan\">index.html<\/code>\u00a0file.<\/p>\n<p>You&#8217;ll notice a single\u00a0<code class=\"w3-codespan\">&lt;div&gt;<\/code>\u00a0in the body of this file. This is where our React application will be rendered.<\/p>\n<h3>Example<\/h3>\n<p>Display a paragraph inside an element with the id of &#8220;root&#8221;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">const container = document.getElementById('root');\r\nconst root = ReactDOM.createRoot(container);\r\nroot.render(&lt;p&gt;Hello&lt;\/p&gt;);<\/pre>\n<p>The result is displayed in the\u00a0<code class=\"w3-codespan\">&lt;div id=\"root\"&gt;<\/code>\u00a0element:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;body&gt;\r\n  &lt;div id=\"root\"&gt;&lt;\/div&gt;\r\n&lt;\/body&gt;<\/pre>\n<p><span style=\"color: #ff0000;\">Note that the element id does not have to be called &#8220;root&#8221;, but this is the standard convention.<\/span><\/p>\n<h2>The HTML Code<\/h2>\n<p>The HTML code in this tutorial uses JSX which allows you to write HTML tags inside the JavaScript code:<\/p>\n<p>Do not worry if the syntax is unfamiliar, you will learn more about JSX in the next chapter.<\/p>\n<h3>Example<\/h3>\n<p>Create a variable that contains HTML code and display it in the &#8220;root&#8221; node:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">const myelement = (\r\n  &lt;table&gt;\r\n    &lt;tr&gt;\r\n      &lt;th&gt;Name&lt;\/th&gt;\r\n    &lt;\/tr&gt;\r\n    &lt;tr&gt;\r\n      &lt;td&gt;John&lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n    &lt;tr&gt;\r\n      &lt;td&gt;Elsa&lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n  &lt;\/table&gt;\r\n);\r\n\r\nconst container = document.getElementById('root');\r\nconst root = ReactDOM.createRoot(container);\r\nroot.render(myelement);<\/pre>\n<h2>The Root Node<\/h2>\n<p>The root node is the HTML element where you want to display the result.<\/p>\n<p>It is like a\u00a0<em>container<\/em>\u00a0for content managed by React.<\/p>\n<p>It does NOT have to be a\u00a0<code class=\"w3-codespan\">&lt;div&gt;<\/code>\u00a0element and it does NOT have to have the\u00a0<code class=\"w3-codespan\">id='root'<\/code>:<\/p>\n<div class=\"w3-example\">\n<h3>Example<\/h3>\n<p>The root node can be called whatever you like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;body&gt;\r\n\r\n  &lt;header id=\"sandy\"&gt;&lt;\/header&gt;\r\n\r\n&lt;\/body&gt;<\/pre>\n<p>Display the result in the\u00a0<code class=\"w3-codespan\">&lt;header id=\"sandy\"&gt;<\/code>\u00a0element:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">const container = document.getElementById('sandy');\r\nconst root = ReactDOM.createRoot(container);\r\nroot.render(&lt;p&gt;Hallo&lt;\/p&gt;);<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>React&#8217;s goal is in many ways to render HTML in a web <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/react-react-render-html\/\" title=\"React &#8211; React Render HTML\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":4376,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[508,12],"tags":[516,514,517,511,510,491,519,509,513,529,512,518,520,515],"newstopic":[578],"class_list":{"0":"post-4209","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-react","8":"category-web-development","9":"tag-array-methods","10":"tag-arrow-functions","11":"tag-destructuring","12":"tag-ecmascript-6","13":"tag-es6-tutorial","14":"tag-javascript","15":"tag-modules","16":"tag-react","17":"tag-react-classes","18":"tag-react-render-html","19":"tag-react-tutorial","20":"tag-spread-operator","21":"tag-ternary-operator","22":"tag-variables","23":"newstopic-react"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=4209"}],"version-history":[{"count":2,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4209\/revisions"}],"predecessor-version":[{"id":4213,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4209\/revisions\/4213"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/4376"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=4209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=4209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=4209"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=4209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}