{"id":29581,"date":"2022-05-27T07:16:00","date_gmt":"2022-05-27T07:16:00","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=29581"},"modified":"2022-06-13T10:05:56","modified_gmt":"2022-06-13T10:05:56","slug":"types-of-react-components","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/types-of-react-components.html","title":{"rendered":"Types of ReactJs Components in Programming"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Are_Components_in_ReactJs\"><\/span>What Are Components in ReactJs?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The components basically refer to the fundamental unit and building blocks of React. To make it easier to understand, just like anything in the world is made up of atoms, the components are just the constituents of ReactJS. Any application or website being developed using <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/react-native-for-app-development.html\">ReactJS<\/a> will be made up of the different units which are called the components.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"499\" src=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/05\/UI-Components-1024x499.jpg\" alt=\"UI Components\" class=\"wp-image-29583\" srcset=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/05\/UI-Components-1024x499.jpg 1024w, https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/05\/UI-Components-300x146.jpg 300w, https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/05\/UI-Components-768x375.jpg 768w, https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/05\/UI-Components-1536x749.jpg 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the image shown above, you can find several components on the UI of the website. The Menu bar is one component while the left Featured column is another. The cookie box is also a component and the main body providing all the information is again just another. So the whole UI is made up of the different components to make the website useful for us. The UI of the website thus can be considered as the parent component.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Types_of_ReactJs_Components_in_Programming\"><\/span>Types of ReactJs Components in Programming&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Functional_Component\"><\/span>1. Functional Component&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Functional components can be created simply by writing the Javascript functions. To be specific, the functional components are those functions that take the Props in and return the JSX. The functional components might or might not be able to receive the data in form of parameters. Moreover, the functional components don&#8217;t have the lifecycle methods or state however they can be added by simply implementing the React Hooks. The functional components are always easy to debug, read and test.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Functional Component Example\nimport React from 'react';\nconst HelloWorld = () =&gt; {\n   return (\n      &lt;div&gt;\n         &lt;p&gt;Hello Emizen!&lt;\/p&gt;\n      &lt;\/div&gt;\n   )\n}\nexport default HelloEmizen;<\/code><\/pre>\n\n\n\n<p>In the above code image, it is a simple component carrying the constant variable &#8220;Hello Emizen&#8221;. The constant variable is assigned to the arrow function that returns the JSX. The functional components don\u2019t require to be an arrow function and they can also be simply declared with the regular JavaScript functions. At the same time, Props can also be passed into the function and thus used to render the data in JSX code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Class_Component\"><\/span>2. Class Component&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The Class component is the most used component among all the types. The reason is that the class component has the capability of performing every function of the functional component and at the same time some additional capabilities as well. It can efficiently make use of the main functions of React, props, state and lifecycle methods as well. However, the class components are comparatively more complex compared to that of functional components. The data can be passed from one class component to another class component very easily.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Class Component Exampleimport React from 'react';class HelloWorld extends React.Component {\n   render() {\n      return (\n         &lt;div&gt;\n            &lt;p&gt;Hello Emizen!&lt;\/p&gt;\n         &lt;\/div&gt;\n      )\n   }\n}export default HelloEmizen;<\/pre>\n\n\n\n<p>It can be observed in the above example that the class component uses the <code>extends React.Component<\/code> after the class <code>Hello Emizen<\/code>. Then, it also requires the <code>render()<\/code> method to return the JSX code. In the class component, one can declare a state, set it to the JavaScript object, and use <code>props<\/code> to be in the initial stage and change the state in the lifecycle methods. It will require the React Hooks to perform these actions by functional component.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Pure_Component\"><\/span>3. Pure Component<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Pure components are the most simple and fast components that someone can write. As pure components don\u2019t modify or depend on the state of different variables outside of their scope, they can easily replace the simple functional components. The components which merely return the render function are most adequate for pure components. The biggest use case of the Pure component is to provide optimizations.&nbsp;<\/p>\n\n\n\n<p>The big difference between the <code>React.Component<\/code> and <code>React.PureComponent<\/code> is that the pure component exhibits <strong>shallow comparison <\/strong>on the state change. The pure components automatically manage the <code>shouldComponentUpdate()<\/code>.<\/p>\n\n\n\n<p>React Components are re-rendered mostly when:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>setState()<\/code> Is called<\/li><li><code>forceUpdate()<\/code> Is called&nbsp;<\/li><li><code>props<\/code> Values are updated<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Higher-Order_Components\"><\/span>4. Higher-Order Components<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Higher-order components also known as HOC are less like a React component and more like a pattern, the result of React\u2019s Compositional nature. The main use case of HOC is to share the logic with other components.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ HOC Example\nimport React from 'react';\nimport MyComponent from '.\/components\/MyComponent';\nclass HelloEmizen extends React.Component {\n   render() {\n      return(\n         &lt;div&gt;\n            {this.props.myArray.map((element) =&gt; (\n               &lt;MyComponent data={element} key={element.key} \/&gt;\n            ))}\n         &lt;\/div&gt;\n      )\n   }\n}\nexport default HelloEmizen;<\/code><\/pre>\n\n\n\n<p>The above image of code has the simple component to describe the higher-level component. Here the key code is <code>this.props.myArray.map((element) =&gt; (&lt;MyComponent \/&gt;)<\/code>. This function returns the components. The number of components are simply dependent on the number of elements in the array which are called HOC. The function takes an array from state and thus maps each element in the array by transforming every element into the React Component.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Followings are the Higher-Order Component Simple Rundown:&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Get the data from props or state<\/li><li>Map over the array and return the Reach component for every element.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Render_a_ReactJs_Component\"><\/span>How to Render a ReactJs Component?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Following steps can be followed to render a ReactJS component:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First of all, an ES6 class is created with the same name which extends <code>React.Component<\/code>.&nbsp;<\/li><li>A single empty method is added to it known as <code>render()<\/code>.<\/li><li>Body of the function is moved into the <code>render()<\/code> method.<\/li><li>Replace the <code>props<\/code> with <code>this.props<\/code> in <code>render()<\/code> body.<\/li><li>In the end, delete the remaining empty function declaration.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"React_Nesting_Components\"><\/span>React Nesting Components<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>One of the best things about React is that the components can nest inside one another. This feature helps significantly to create a complex User Interface. Here, the child components are nested inside the parent components. The nesting of components in one another is referred to as internal and external.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Export<\/strong>: when a particular module or file is exported and used in another module, it is called Export.&nbsp;<\/li><li><strong>Import<\/strong>: when a particular module or file is imported and used in the existing module, it is called import.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"React_Component_Lifecycle\"><\/span>React Component Lifecycle<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The React web apps are basically the collection of the different components which are independent and run based on interactions made with them. Each of these components has its own lifecycle. Life Cycle refers to the series of methods that reinvoke in the different stages of the component\u2019s existence.&nbsp;<\/p>\n\n\n\n<p>Any React component basically has the following three stages:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Mounting\"><\/span>1. Mounting<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Once the initialization of components has been completed, the component is then mounted on DOM and rendered on the webpage. Here, React follows the default procedure only for the Naming Conventions of the predefined functions. The functions that contain \u201cWill\u201d represent before some specific phase. Similarly, the function that contains \u201cDid\u201d represents after the completion of the phase. The mounting phase of the components consists of the following two functions:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>componentWillMount() Function<\/strong>: this function is invoked before the component is mounted on DOM.&nbsp;<\/li><li><strong>componentDidMount() Function<\/strong>: opposite to the previous one, this function is invoked after the component is mounted on the DOM.&nbsp;<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Updation\"><\/span>2. Updation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Updation is basically the phase in which the states and props of a component are updated followed by the event or command from the user such as pressing the key, clicking on the mouse, etc. It helps the active web pages to behave according to the needs and commands of their users.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Unmounting\"><\/span>3. Unmounting&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It is the completion stage of the component lifecycle. In this phase, the components are unmounted from the DOM. Following function is the only member of this function:&nbsp;<\/p>\n\n\n\n<p><strong>componentWillUnmount() Function<\/strong>: the function is invoked just before the component is completely unmounted from DOM. It occurs before the component is removed from the page and thus this donates the end of the life cycle.<\/p>\n\n\n\n<p>We hope this post gave you a basic idea of types of React components and how they can be used. Emizentech is a leading <a href=\"https:\/\/multisitelocal.ezxdemo.com\/app-development-guide.html\">app development<\/a> firm that develops web, native and hybrid mobile apps. So, if you ever need professional <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/how-to-hire-react-native-developer.html\">ReactJs developers<\/a> then do get in touch with us. <\/p>\n\n\n\n<div class=\"wp-block-genesis-blocks-gb-accordion gb-block-accordion\"><details><summary class=\"gb-accordion-title\"><strong>You may also like to read<\/strong><\/summary><div class=\"gb-accordion-text\">\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/flutter-vs-react-native.html\">Flutter vs. React Native What To Choose For App Development<\/a><\/li><li><a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/angular-vs-react.html\">Angular vs. React: Differences, Which Js Framework is Better?<\/a><\/li><li><a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/vue-vs-react.html\">Vue vs. React: What is The Best JavaScript Framework?<\/a><\/li><li><a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/next-js.html\">What is Next.js &amp; Why &amp; How To Use It?<\/a><\/li><\/ul>\n<\/div><\/details><\/div>\n","protected":false},"excerpt":{"rendered":"<p>What Are Components in ReactJs? The components basically refer to the fundamental unit and building blocks of React. To make it easier to understand, just like anything in the world is made up of atoms, the components are just the constituents of ReactJS. Any application or website being developed using ReactJS will be made up<\/p>\n","protected":false},"author":35,"featured_media":29584,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"MSN_Categories":"Uncategorized","MSN_Publish_Option":false,"MSN_Is_Local_News":false,"MSN_Is_AIAC_Included":"Empty","MSN_Location":"[]","MSN_Add_Feature_Img_On_Top_Of_Post":false,"MSN_Has_Custom_Author":false,"MSN_Custom_Author":"","MSN_Has_Custom_Canonical_Url":false,"MSN_Custom_Canonical_Url":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[81],"tags":[],"class_list":{"0":"post-29581","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-mobile-app-development"},"modified_by":"Marketing EmizenTech","featured_image_src":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/05\/types-of-react-components-600x400.jpg","featured_image_src_square":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/05\/types-of-react-components-600x600.jpg","author_info":{"display_name":"Amit Samsukha","author_link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/author\/amit"},"_links":{"self":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/29581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/comments?post=29581"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/29581\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media\/29584"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=29581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=29581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=29581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}