{"id":5076,"date":"2020-09-28T16:08:51","date_gmt":"2020-09-28T10:38:51","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=5076"},"modified":"2024-06-06T17:47:00","modified_gmt":"2024-06-06T12:17:00","slug":"understanding-components","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/understanding-components\/","title":{"rendered":"Understanding Components"},"content":{"rendered":"\n<p>Components are one of the core building blocks of React. Every application developed in React is made up of pieces known as components. Components make the task of configuring UIs much easier. You can break a UI into multiple individual pieces called components, work on them independently, and then merge all of them in a parent component, which will become your final UI.<\/p>\n\n\n\n<p>Components in React return a piece of JSX code that tells what should be displayed on the screen. There are two types of components in the React:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Functional Components:<\/strong> Functional components are JavaScript functions and can be created by writing a JavaScript function. Those functions may or may not receive data as parameters. Given below is a valid functional component in React:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">function Democomponent()\n{\n&nbsp;&nbsp;&nbsp;&nbsp;return &lt;h1&gt;Welcome Message!&lt;\/h1&gt;;\n}\n<\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Class Components:<\/strong> The class components are complex than the functional components. The functional components do not know the other components in the program, whereas the class components work with each other. We can also pass data from one class component to other class components and can use JavaScript ES6 classes to create class-based components. Given below is a valid class-based component in React:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">class Democomponent extends React.Component\n{\n&nbsp;&nbsp;&nbsp;&nbsp;render(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &lt;h1&gt;Welcome Message!&lt;\/h1&gt;;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<p>We will use functional components only when we are sure that our component does not require interacting or working with any other component, i.e., these components do not require data from other components. But, we can compose multiple functional components under a single functional component. We can use class-based components for this purpose, but it&#8217;s not recommended, as using class-based components without need will make your application inefficient.<\/p>\n\n\n\n<p>React is capable of rendering user-defined components. To render a React component, we can initialize an element with a user-defined component and then either pass this element as the first parameter to ReactDOM.render() or can directly pass the component as the first argument in the ReactDOM.render() method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Syntax to initialize a component to an element:<\/strong><\/h2>\n\n\n\n<p><code>const elementName = &lt;ComponentName \/&gt;;<\/code><\/p>\n\n\n\n<p>Here, the ComponentName is the name of the user-defined component. The name of a component should always start with a capital letter. This is done to differentiate a component tag with HTML tags.<\/p>\n\n\n\n<p>Below example renders a component named Welcome to the screen:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React from 'react';&nbsp;\nimport ReactDOM from 'react-dom';&nbsp;&nbsp;&nbsp;\n\/\/ This is a functional component&nbsp;\nfunction Welcome()&nbsp;\n{&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &lt;h1&gt;Hello World!&lt;\/h1&gt;&nbsp;\n}&nbsp;&nbsp;&nbsp;&nbsp;\nReactDOM.render(&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;Welcome \/&gt;,&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;document.getElementById(\"root\")&nbsp;\n);\n<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/xtarjSXbSKG-IiReHaXLQu1Fv1AyYU3VSs5GPh0HuBMgF7hLdPcDDhTOoZR0N_bc5zwxNjq_WZk1-U4PNqUxon8VVw6xIDYTCfVzfo8F8uWaQrkd7Gpbj9NCvDfZNvZEtPCAhtoEGQmQf8aNEg\" alt=\"initialize a component to an element\" title=\"\"><\/figure>\n\n\n\n<p>Here, we are calling the ReactDOM.render() with the first parameter.<\/p>\n\n\n\n<p>React then calls the component Welcome, which returns &lt;h1&gt;Hello World!&lt;\/h1&gt;; as a result.<\/p>\n\n\n\n<p>Then the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/create-hello-world-react-app\/\">ReactDOM<\/a> efficiently updates the DOM to match with the returned element and renders that element to the DOM element with id as \u201croot.\u201d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Create a Class Component:<\/strong><\/h2>\n\n\n\n<p>While creating a React component, the component&#8217;s name must start with an upper case letter. The component also have to include the extends React.Component statement, as it will create an inheritance to React.Component, and gives your component access to the functions of React.Component.<\/p>\n\n\n\n<p><strong><strong>Step 1: <\/strong>Create a Class component named Car<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Car extends React.Component {\n&nbsp;&nbsp;render() {\n&nbsp;&nbsp;&nbsp;&nbsp;return &lt;h2&gt;I am a Car!&lt;\/h2&gt;;\n&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<p>Now, the React application has a component named Car, which returns an &lt;h2&gt; element.<\/p>\n\n\n\n<p><strong>Step 2: <\/strong>Display the Car component in the &#8220;root&#8221; element:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ReactDOM.render(&lt;Car \/&gt;, document.getElementById('root'));<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Create a Function Component:<\/strong><\/h2>\n\n\n\n<p>This is the same example as used above, but it is created using a Function component instead.<\/p>\n\n\n\n<p>A Function component also returns HTML and it behaves pretty much the same way as a Class component.<\/p>\n\n\n\n<p><strong>Step 1: <\/strong>Create a Function component called Car<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function Car() {\n&nbsp;&nbsp;return &lt;h2&gt;I am also a Car!&lt;\/h2&gt;;\n}\n<\/pre>\n\n\n\n<p>Once again, the React application has a Car component.<\/p>\n\n\n\n<p><strong>Step 2: <\/strong>Display the Car component in the &#8220;root&#8221; element:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ReactDOM.render(&lt;Car \/&gt;, document.getElementById('root'));\n<\/pre>\n\n\n\n<p><strong>Component Constructor: <\/strong>If there is also a constructor() function in your component, this function will be called when it is initiated. The constructor function is the code where you initiate the component&#8217;s properties. In React, the component properties should be kept in an object known as state.<\/p>\n\n\n\n<p>The constructor function is also a code where you honor the parent component&#8217;s inheritance by including the super() statement that executes the constructor function of parent component, and your component gets access to all those functions of the parent component.<\/p>\n\n\n\n<p><strong>Example: <\/strong>Create a constructor function, and add a property color:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Car extends React.Component {\n&nbsp;&nbsp;constructor() {\n&nbsp;&nbsp;&nbsp;&nbsp;super();\n&nbsp;&nbsp;&nbsp;&nbsp;this.state = {color: \"red\"};\n&nbsp;&nbsp;}\n&nbsp;&nbsp;render() {\n&nbsp;&nbsp;&nbsp;&nbsp;return &lt;h2&gt;I am a Car!&lt;\/h2&gt;;\n&nbsp;&nbsp;}\n}\nUse the property color in the render() function:\nclass Car extends React.Component {\n&nbsp;&nbsp;constructor() {\n&nbsp;&nbsp;&nbsp;&nbsp;super();\n&nbsp;&nbsp;&nbsp;&nbsp;this.state = {color: \"red\"};\n&nbsp;&nbsp;}\n&nbsp;&nbsp;render() {\n&nbsp;&nbsp;&nbsp;&nbsp;return &lt;h2&gt;I am a {this.state.color} Car!&lt;\/h2&gt;;\n&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<p><strong>Props: <\/strong>Another way of handling properties of component is by using props. They are like <a href=\"https:\/\/web-archive.southampton.ac.uk\/journals.ecs.soton.ac.uk\/\" rel=\"nofollow noopener\" target=\"_blank\">function arguments<\/a>, and you send them into the component as attributes.<\/p>\n\n\n\n<p><strong>Example: <\/strong>Use an attribute to pass color to the Car component, and use it in the render() function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Car extends React.Component {\n&nbsp;&nbsp;render() {\n&nbsp;&nbsp;&nbsp;&nbsp;return &lt;h2&gt;I am a {this.props.color} Car!&lt;\/h2&gt;;\n&nbsp;&nbsp;}\n}\nReactDOM.render(&lt;Car color=\"red\"\/&gt;, document.getElementById('root'));\n<\/pre>\n\n\n\n<p><strong>Components in Components: <\/strong>We can create components inside other components.<\/p>\n\n\n\n<p><strong>Example: <\/strong>Using the Car component inside the Garage component:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Car extends React.Component {\n&nbsp;&nbsp;render() {\n&nbsp;&nbsp;&nbsp;&nbsp;return &lt;h2&gt;I am a Car!&lt;\/h2&gt;;\n&nbsp;&nbsp;}\n}\nclass Garage extends React.Component {\n&nbsp;&nbsp;render() {\n&nbsp;&nbsp;&nbsp;&nbsp;return (\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h1&gt;Who lives in my Garage?&lt;\/h1&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Car \/&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/div&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;);\n&nbsp;&nbsp;}\n}\nReactDOM.render(&lt;Garage \/&gt;, document.getElementById('root'));\n<\/pre>\n\n\n\n<p><strong>Components in Files: <\/strong>React is actually re-using the code, and it can be smart to insert some of your components in separate files. For this, create a new file with a .js file extension and put the code inside it.<\/p>\n\n\n\n<p>The file must start by importing React, and it has to end with the statement export default Car.<\/p>\n\n\n\n<p><strong>Step 1: <\/strong>This is the new file, we named it &#8220;App.js&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React from 'react';\nimport ReactDOM from 'react-dom';\nclass Car extends React.Component {\n&nbsp;&nbsp;render() {\n&nbsp;&nbsp;&nbsp;&nbsp;return &lt;h2&gt;I am a Car!&lt;\/h2&gt;;\n}\n}\nexport default Car;\n<\/pre>\n\n\n\n<p>To use the component Car, you have to import the file in your application.<\/p>\n\n\n\n<p><strong>Step 2: <\/strong>Now, we will import the &#8220;App.js&#8221; file in the application, and use the Car component as if it is created here.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React from 'react';\nimport ReactDOM from 'react-dom';\nimport Car from '.\/App.js';\nReactDOM.render(&lt;Car \/&gt;, document.getElementById('root'));\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Components are one of the core building blocks of React. Every application developed in React is made up of pieces known as components. Components make the task of configuring UIs much easier. You can break a UI into multiple individual pieces called components, work on them independently, and then merge all of them in a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7205,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-5076","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/5076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=5076"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/5076\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/7205"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=5076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=5076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=5076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}