{"id":1777,"date":"2020-09-07T13:46:52","date_gmt":"2020-09-07T08:16:52","guid":{"rendered":"http:\/\/uitutorials.in\/wp\/?p=1777"},"modified":"2020-09-07T13:46:52","modified_gmt":"2020-09-07T08:16:52","slug":"basic-concept-of-react-component","status":"publish","type":"post","link":"https:\/\/uitutorials.in\/wp\/basic-concept-of-react-component\/","title":{"rendered":"Basic Concept Of React Component"},"content":{"rendered":"<h3>1 What is React.js<\/h3>\n<p>React, or React.js is an open source JavaScript framework from Facebook. React.js is ideal for doing view rendering work in large scale or single page application (SPA). That is, React works as the V in MVC.<\/p>\n<h3>2 How React works<\/h3>\n<p>The main concept of React is component. A React component can be composed of React components and ReactElement. Following the React way, you divide your app into several components according to function and responsibility.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/03-concepts_of_reactjs_component.png\" alt=\"\" width=\"677\" height=\"529\" class=\"alignnone size-full wp-image-1778\" srcset=\"https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/03-concepts_of_reactjs_component.png 677w, https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/03-concepts_of_reactjs_component-300x234.png 300w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><br \/>\nReact components and ReactElement are used as Virtual DOM, which looks like the markup of real HTML DOM elements.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/04-how_reactjs_work.png\" alt=\"\" width=\"893\" height=\"360\" class=\"alignnone size-full wp-image-1779\" srcset=\"https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/04-how_reactjs_work.png 893w, https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/04-how_reactjs_work-300x121.png 300w, https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/04-how_reactjs_work-768x310.png 768w\" sizes=\"auto, (max-width: 893px) 100vw, 893px\" \/><br \/>\nVirtual DOM (components and ReactElements) syntax will be compiled and translated into plain JavaScript codes, which will render real HTML DOM elements in browser.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/05-reactjs_render_component_virtual_dom.png\" alt=\"\" width=\"936\" height=\"306\" class=\"alignnone size-full wp-image-1780\" srcset=\"https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/05-reactjs_render_component_virtual_dom.png 936w, https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/05-reactjs_render_component_virtual_dom-300x98.png 300w, https:\/\/uitutorials.in\/wp\/wp-content\/uploads\/2020\/09\/05-reactjs_render_component_virtual_dom-768x251.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/p>\n<h3>3 Write a React component<\/h3>\n<p>As mentioned above, two things are required to write and run a React component.<\/p>\n<p>1.React.js<br \/>\n2.babel, a translator, can translate ECMAScript 6 codes and Virtual DOM syntax to ECMAScript 5 codes, which can be run in all modern browsers.<br \/>\nFor simplicity, we&#8217;re going to use React and babel scripts hosted by free CDN.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;title&gt;React Demo&lt;\/title&gt;\r\n\r\n    &lt;!-- React includes two parts: react.js and react-dom.js, both are required --&gt;\r\n    &lt;script src=\"\/\/fb.me\/react-0.14.7.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"\/\/fb.me\/react-dom-0.14.7.min.js\"&gt;&lt;\/script&gt;\r\n\r\n    &lt;!-- babel standalone --&gt;\r\n    &lt;script src=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/babel-standalone\/6.10.3\/babel.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div id=\"app\"&gt;&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<p>Create a HelloWorld component which is responsible for rendering a HTML h3 tag and a HTML p tag. Then mount the HelloWorld component to div tag whose id is app.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;title&gt;React Demo&lt;\/title&gt;\r\n\r\n    &lt;!-- React includes two parts: react.js and react-dom.js, both are required --&gt;\r\n    &lt;script src=\"\/\/fb.me\/react-0.14.7.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"\/\/fb.me\/react-dom-0.14.7.min.js\"&gt;&lt;\/script&gt;\r\n\r\n    &lt;!-- babel standalone --&gt;\r\n    &lt;script src=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/babel-standalone\/6.10.3\/babel.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div id=\"app\"&gt;&lt;\/div&gt;\r\n\r\n    &lt;script type=\"text\/babel\"&gt;\r\n        var HelloWorld = React.createClass({\r\n            \/\/ render function of React component, render real DOM\r\n            render: function () {\r\n                return (\r\n                    &lt;div&gt;\r\n                        &lt;h3&gt;Hello&lt;\/h3&gt;\r\n                        &lt;p&gt;Nice to meet you!&lt;\/p&gt;\r\n                    &lt;\/div&gt;\r\n                )\r\n            }\r\n        });\r\n\r\n        ReactDOM.render(&lt;HelloWorld \/&gt;, document.getElementById('app'));\r\n    &lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<p>Pay attention to type attribute of script tag. Its value is text\/babel, which means that the codes should be processed and translated by babel.<\/p>\n<p><strong>Note:<\/strong> The net result of running above code snippets is that React will render real HTML DOM tags, div, h3 and p in browser. But div, h3 and p in above codes are not real HTML DOMs, they are also virtual DOMs. They are also React elements, that is ReactElement exactly in React&#8217;s eyes.<\/p>\n<p>Take the <\/p>\n<h3>Hello<\/h3>\n<p> element in above render() function as an example, React.js will create a ReactElement essentially. Equivalently, the behind the scenes code are as follows.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ h3 is an instance of ReactElement\r\nvar h3 = React.createElement('h3', null, 'Hello');<\/code><\/pre>\n<p>As you can see, a react component can be embedded as a virtual DOM within another component. For another example, you can give a root component, e.g. App, and then use all sub components and ReactElements within this root.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;title&gt;React Demo&lt;\/title&gt;\r\n    &lt;!-- react includes two parts: react.js and react-dom.js --&gt;\r\n    &lt;script src=\"\/\/fb.me\/react-0.14.7.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"\/\/fb.me\/react-dom-0.14.7.min.js\"&gt;&lt;\/script&gt;\r\n\r\n    &lt;!-- babel standalone --&gt;\r\n    &lt;script src=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/babel-standalone\/6.10.3\/babel.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div id=\"app\"&gt;&lt;\/div&gt;\r\n\r\n    &lt;script type=\"text\/babel\"&gt;\r\n        var HelloWorld = React.createClass({\r\n            \/\/ render function of React component\r\n            render: function () {\r\n                return (\r\n                    &lt;div&gt;\r\n                        &lt;h3&gt;Hello&lt;\/h3&gt;\r\n                        &lt;p&gt;Nice to meet you!&lt;\/p&gt;\r\n                    &lt;\/div&gt;\r\n                )\r\n            }\r\n        });\r\n\r\n        var App = React.createClass({\r\n            render: function () {\r\n                return (\r\n                    &lt;div&gt;\r\n                        &lt;h1&gt;React Demo App&lt;\/h1&gt;\r\n                        &lt;HelloWorld \/&gt;\r\n                    &lt;\/div&gt;\r\n                )\r\n            }\r\n        });\r\n\r\n        \/\/ Mount root App component\r\n        ReactDOM.render(&lt;App \/&gt;, document.getElementById('app'));\r\n    &lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>4 CSS class and inline styles of React component<\/h3>\n<p>The usual way of adding CSS class name and inline styles will not take effect in React component.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;title&gt;React Demo&lt;\/title&gt;\r\n    &lt;!-- react includes two parts: react.js and react-dom.js --&gt;\r\n    &lt;script src=\"\/\/fb.me\/react-0.14.7.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"\/\/fb.me\/react-dom-0.14.7.min.js\"&gt;&lt;\/script&gt;\r\n\r\n    &lt;!-- babel standalone --&gt;\r\n    &lt;script src=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/babel-standalone\/6.10.3\/babel.min.js\"&gt;&lt;\/script&gt;\r\n\r\n    &lt;style type=\"text\/css\"&gt;\r\n    &lt;!--\r\n        .greet { color: red}\r\n    --&gt;\r\n    &lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div id=\"app\"&gt;&lt;\/div&gt;\r\n\r\n    &lt;script type=\"text\/babel\"&gt;\r\n        var HelloWorld = React.createClass({\r\n            render: function () {\r\n                return (\r\n                    &lt;div&gt;\r\n                        &lt;h3 style=\"color:blue;\"&gt;Hello&lt;\/h3&gt;\r\n                        &lt;p class=\"greet\"&gt;Nice to meet you!&lt;\/p&gt;\r\n                    &lt;\/div&gt;\r\n                )\r\n            }\r\n        });\r\n\r\n        var App = React.createClass({\r\n            render: function () {\r\n                return (\r\n                    &lt;div&gt;\r\n                        &lt;h1&gt;React Demo App&lt;\/h1&gt;\r\n                        &lt;HelloWorld \/&gt;\r\n                    &lt;\/div&gt;\r\n                )\r\n            }\r\n        });\r\n\r\n        \/\/ Mount root App component\r\n        ReactDOM.render(&lt;App \/&gt;, document.getElementById('app'));\r\n    &lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<p>The inline style color:blue; and CSS class greet not only won&#8217;t work, but will raise JavaScript error. (See it in JavaScript console of developer tool in your browser.)<\/p>\n<p>Just as I have said, h3 and p here are ReactElements instead of real HTML DOM elements.<\/p>\n<p>The correct way to set a CSS class name is to use className property of React components or ReactElements.<\/p>\n<p>Likewise, inline CSS styles should be wrapped as an object and passed to the style property of React components.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;script type=\"text\/babel\"&gt;\r\n\/\/ ...\r\n\r\nvar HelloWorld = React.createClass({\r\n    render: function () {\r\n        return (\r\n            &lt;div&gt;\r\n                &lt;h3 style={ {color: 'blue'} }&gt;Hello&lt;\/h3&gt;\r\n                &lt;p className=\"greet\"&gt;Nice to meet you!&lt;\/p&gt;\r\n            &lt;\/div&gt;\r\n        )\r\n    }\r\n});\r\n\r\n\/\/ ...\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>Alternatively, if the inline styles are long and complex, you can predefine styles as a variable.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">&lt;script type=\"text\/babel\"&gt;\r\n\/\/ ...\r\n\r\nvar HelloWorld = React.createClass({\r\n    render: function () {\r\n        var styleObj = {\r\n            color: 'blue',\r\n            \/\/ font-size in CamelCase\r\n            fontSize: '40px'\r\n        };\r\n\r\n        return (\r\n            &lt;div&gt;\r\n                &lt;h3 style={ styleObj }&gt;Hello&lt;\/h3&gt;\r\n                &lt;p className=\"greet\"&gt;Nice to meet you!&lt;\/p&gt;\r\n            &lt;\/div&gt;\r\n        )\r\n    }\r\n});\r\n\r\n\/\/ ...\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>Note that the CSS property name should be written in CamelCase way, just as the way in plain JavaScript.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ CamelCase for padding-left is: paddingLeft\r\ndocument.getElementById('demo').style.paddingLeft = '30px';<\/code><\/pre>\n<h3>Reference<\/h3>\n<p><a href=\"https:\/\/www.codevoila.com\/post\/38\/react-tutorial-basic-concept-of-react-component\" rel=\"noopener\" target=\"_blank\">codevoila<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 What is React.js React, or React.js is an open source JavaScript framework from Facebook. React.js is ideal for doing view rendering work in large scale or single page application (SPA). That is, React works as the V in MVC. 2 How React works The main concept of React is<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106],"tags":[107],"class_list":["post-1777","post","type-post","status-publish","format-standard","hentry","category-react","tag-react","ct-col-2"],"_links":{"self":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/1777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/comments?post=1777"}],"version-history":[{"count":1,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/1777\/revisions"}],"predecessor-version":[{"id":1781,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/1777\/revisions\/1781"}],"wp:attachment":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/media?parent=1777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/categories?post=1777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/tags?post=1777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}