{"id":3583,"date":"2023-01-04T11:34:13","date_gmt":"2023-01-04T06:04:13","guid":{"rendered":"https:\/\/www.cloudnowtech.com\/blog\/?p=3583"},"modified":"2023-01-04T11:34:13","modified_gmt":"2023-01-04T06:04:13","slug":"understanding-higher-order-components-in-react-js","status":"publish","type":"post","link":"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/","title":{"rendered":"Understanding higher-order components in React.js"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Higher-order components (HOCs) are an advanced topic in React.js, often used by <\/span><span style=\"font-weight: 400;\">application development experts<\/span><span style=\"font-weight: 400;\"> to streamline the code and make the development project more efficient. A <\/span><a href=\"https:\/\/reactjs.org\/docs\/higher-order-components.html\"><span style=\"font-weight: 400;\">HOC<\/span><\/a><span style=\"font-weight: 400;\"> is a \u201cfunction that takes a component and returns a new component\u201d. It reuses component logic and follows one of the patterns.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">When do you use HOCs?<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Let\u2019s say you have a box to which you want to add an outline color. A component would perform this activity. Now let\u2019s say you have ten similar boxes, each needing an outline color. You could hand-write code for each box, or use a HOC to create a function that provides an outline color for each.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Here\u2019s an example of a function that creates a new component: a HOC<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">import React from \u2018react\u2019<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">const higherOrderComponent = WrappedComponent =&gt; {<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0class HOC extends React.Component {<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0render() {<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return &lt;WrappedComponent \/&gt;<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0}<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">\u00a0\u00a0return HOC<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">}<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">In the above example, a higher-order component is written as a function. It takes a component, called \u2018WrappedComponent\u2019, as an argument. We have created a new component called \u2018HOC\u2019, which returns the &lt;WrappedComponent\/&gt; from its render function.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">A higher-order component (HOC) is a distinctive element for reusing logic in React components.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">How do you use Higher-Order Components?<\/span><\/h3>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">HOCs don\u2019t modify or mutate components; they create new ones<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">The code can be reused as required<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">No side effects arise from the use of HOCs in <\/span><span style=\"font-weight: 400;\">React.js development services<\/span><\/li>\n<\/ol>\n<h3><span style=\"font-weight: 400;\">What are the benefits of using HOCs?<\/span><\/h3>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">When used properly and efficiently, HOCs are easy to handle<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">HOCs help to streamline the code by avoiding mutating or comoving the same logic in each component created<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Using higher-order components makes code more readable and efficient<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Due to its proper naming conventions, debugging becomes easy<\/span><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><i><span style=\"font-weight: 400;\">HOCs can make your code lighter, faster to develop, easier to read and more streamlined. Still wondering how to use HOCs when using <\/span><\/i><i><span style=\"font-weight: 400;\">React.js development services<\/span><\/i><i><span style=\"font-weight: 400;\">? Speak to our team of <\/span><\/i><i><span style=\"font-weight: 400;\">application development experts<\/span><\/i><i><span style=\"font-weight: 400;\"> today.<\/span><\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Higher-order components (HOCs) are an advanced topic in React.js, often used by application development experts to streamline the code and make the development project more efficient. A HOC is a \u201cfunction that takes a component and returns a new component\u201d. It reuses component logic and follows one of the patterns. When do you use HOCs? [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":3584,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[1],"tags":[362,361,363],"class_list":["post-3583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-others","tag-higher-order-components","tag-hoc","tag-react-js"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v17.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Understanding higher-order components in React.js - Discover Better Value Faster<\/title>\n<meta name=\"description\" content=\"Higher-order components (HOCs) in React.js are used to streamline code and make development projects more efficient.We explore what HOCs are and how they can help you in your next project.#reactjs #uiux #appdevelopment #CloudNow\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding higher-order components in React.js - Discover Better Value Faster\" \/>\n<meta property=\"og:description\" content=\"Higher-order components (HOCs) in React.js are used to streamline code and make development projects more efficient.We explore what HOCs are and how they can help you in your next project.#reactjs #uiux #appdevelopment #CloudNow\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/\" \/>\n<meta property=\"og:site_name\" content=\"Discover Better Value Faster\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-04T06:04:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2023\/01\/Blog-131.png?fit=1140%2C760&#038;ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"1140\" \/>\n\t<meta property=\"og:image:height\" content=\"760\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Thamothara Kannan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.cloudnowtech.com\/blog\/#website\",\"url\":\"https:\/\/www.cloudnowtech.com\/blog\/\",\"name\":\"Discover Better Value Faster\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.cloudnowtech.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/i0.wp.com\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2023\/01\/Blog-131.png?fit=1140%2C760&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2023\/01\/Blog-131.png?fit=1140%2C760&ssl=1\",\"width\":1140,\"height\":760,\"caption\":\"Understanding higher-order components in React.js\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/#webpage\",\"url\":\"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/\",\"name\":\"Understanding higher-order components in React.js - Discover Better Value Faster\",\"isPartOf\":{\"@id\":\"https:\/\/www.cloudnowtech.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/#primaryimage\"},\"datePublished\":\"2023-01-04T06:04:13+00:00\",\"dateModified\":\"2023-01-04T06:04:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.cloudnowtech.com\/blog\/#\/schema\/person\/4cf3998e754b1675cc0cc955757e18eb\"},\"description\":\"Higher-order components (HOCs) in React.js are used to streamline code and make development projects more efficient.We explore what HOCs are and how they can help you in your next project.#reactjs #uiux #appdevelopment #CloudNow\",\"breadcrumb\":{\"@id\":\"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.cloudnowtech.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Understanding higher-order components in React.js\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.cloudnowtech.com\/blog\/#\/schema\/person\/4cf3998e754b1675cc0cc955757e18eb\",\"name\":\"Thamothara Kannan\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.cloudnowtech.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2021\/12\/mypic-scaled-96x96.jpg\",\"contentUrl\":\"https:\/\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2021\/12\/mypic-scaled-96x96.jpg\",\"caption\":\"Thamothara Kannan\"},\"description\":\"Thamotharakannan is a technology enthusiast and has been working in the tech field for over five years. He has hands-on experience in programming, deployment and requirement analysis. He loves discussing and learning about new cloud innovations and technologies, and his interest lies particularly in debugging.\",\"url\":\"https:\/\/www.cloudnowtech.com\/blog\/author\/tk\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Understanding higher-order components in React.js - Discover Better Value Faster","description":"Higher-order components (HOCs) in React.js are used to streamline code and make development projects more efficient.We explore what HOCs are and how they can help you in your next project.#reactjs #uiux #appdevelopment #CloudNow","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/","og_locale":"en_US","og_type":"article","og_title":"Understanding higher-order components in React.js - Discover Better Value Faster","og_description":"Higher-order components (HOCs) in React.js are used to streamline code and make development projects more efficient.We explore what HOCs are and how they can help you in your next project.#reactjs #uiux #appdevelopment #CloudNow","og_url":"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/","og_site_name":"Discover Better Value Faster","article_published_time":"2023-01-04T06:04:13+00:00","og_image":[{"width":1140,"height":760,"url":"https:\/\/i0.wp.com\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2023\/01\/Blog-131.png?fit=1140%2C760&ssl=1","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Thamothara Kannan","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/www.cloudnowtech.com\/blog\/#website","url":"https:\/\/www.cloudnowtech.com\/blog\/","name":"Discover Better Value Faster","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.cloudnowtech.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/#primaryimage","inLanguage":"en-US","url":"https:\/\/i0.wp.com\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2023\/01\/Blog-131.png?fit=1140%2C760&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2023\/01\/Blog-131.png?fit=1140%2C760&ssl=1","width":1140,"height":760,"caption":"Understanding higher-order components in React.js"},{"@type":"WebPage","@id":"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/#webpage","url":"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/","name":"Understanding higher-order components in React.js - Discover Better Value Faster","isPartOf":{"@id":"https:\/\/www.cloudnowtech.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/#primaryimage"},"datePublished":"2023-01-04T06:04:13+00:00","dateModified":"2023-01-04T06:04:13+00:00","author":{"@id":"https:\/\/www.cloudnowtech.com\/blog\/#\/schema\/person\/4cf3998e754b1675cc0cc955757e18eb"},"description":"Higher-order components (HOCs) in React.js are used to streamline code and make development projects more efficient.We explore what HOCs are and how they can help you in your next project.#reactjs #uiux #appdevelopment #CloudNow","breadcrumb":{"@id":"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.cloudnowtech.com\/blog\/understanding-higher-order-components-in-react-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.cloudnowtech.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Understanding higher-order components in React.js"}]},{"@type":"Person","@id":"https:\/\/www.cloudnowtech.com\/blog\/#\/schema\/person\/4cf3998e754b1675cc0cc955757e18eb","name":"Thamothara Kannan","image":{"@type":"ImageObject","@id":"https:\/\/www.cloudnowtech.com\/blog\/#personlogo","inLanguage":"en-US","url":"https:\/\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2021\/12\/mypic-scaled-96x96.jpg","contentUrl":"https:\/\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2021\/12\/mypic-scaled-96x96.jpg","caption":"Thamothara Kannan"},"description":"Thamotharakannan is a technology enthusiast and has been working in the tech field for over five years. He has hands-on experience in programming, deployment and requirement analysis. He loves discussing and learning about new cloud innovations and technologies, and his interest lies particularly in debugging.","url":"https:\/\/www.cloudnowtech.com\/blog\/author\/tk\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.cloudnowtech.com\/blog\/wp-content\/uploads\/2023\/01\/Blog-131.png?fit=1140%2C760&ssl=1","_links":{"self":[{"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/posts\/3583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/comments?post=3583"}],"version-history":[{"count":2,"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/posts\/3583\/revisions"}],"predecessor-version":[{"id":3635,"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/posts\/3583\/revisions\/3635"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/media\/3584"}],"wp:attachment":[{"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/media?parent=3583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/categories?post=3583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudnowtech.com\/blog\/wp-json\/wp\/v2\/tags?post=3583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}