<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gasmi_Ghassen</title>
    <description>The latest articles on DEV Community by Gasmi_Ghassen (@gasmighassen).</description>
    <link>https://dev.to/gasmighassen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1085613%2F2e71061c-0970-4732-b435-92bcc51d535f.png</url>
      <title>DEV Community: Gasmi_Ghassen</title>
      <link>https://dev.to/gasmighassen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gasmighassen"/>
    <language>en</language>
    <item>
      <title>High order component React js</title>
      <dc:creator>Gasmi_Ghassen</dc:creator>
      <pubDate>Sat, 20 May 2023 23:06:19 +0000</pubDate>
      <link>https://dev.to/gasmighassen/high-order-component-react-js-41cc</link>
      <guid>https://dev.to/gasmighassen/high-order-component-react-js-41cc</guid>
      <description>&lt;p&gt;In React, a higher-order component (HOC) is a function that takes in a component and returns a new component with enhanced functionality. HOCs are a way to reuse and share code among React components that have similar functionality.&lt;/p&gt;

&lt;p&gt;The returned component from a HOC can have additional props, state, or behavior, making it more flexible and reusable across multiple components. HOCs can be used to abstract away common functionality such as state management, lifecycle methods, or handling authentication, and can help to keep code DRY (Don't Repeat Yourself).&lt;/p&gt;

&lt;p&gt;HOCs are commonly used in large-scale React applications to improve code reusability and reduce code duplication. They are a powerful tool for creating modular and composable code in React.&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

&lt;p&gt;In this example we have two components Random1.tsx and Random2.tsx , each component will return a random number between 1 and 100 .&lt;/p&gt;

&lt;p&gt;Random1.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";

type Props = {};

const Random1 = (props: Props) =&amp;gt; {
  const [random, setRandom] = useState(0);
  const handleRand = () =&amp;gt; {
    setRandom(Math.floor(Math.random() * 100));
  };
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Mihed random number {random}&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; handleRand()}&amp;gt;reroll&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Random1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Random2.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";

type Props = {};

const Random2 = (props: Props) =&amp;gt; {
  const [random, setRandom] = useState(0);
  const handleRand = () =&amp;gt; {
    setRandom(Math.floor(Math.random() * 100));
  };
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Foued random number {random}&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; handleRand()}&amp;gt;reroll&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Random2;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see the two components looks the same . HOC provide a solution to not repeat the same logic a cross many components .&lt;/p&gt;

&lt;p&gt;Solution &lt;/p&gt;

&lt;p&gt;We create a component called UpdatedComponents.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";

function UpdatedComponents(ReturnedComponent) {
  function newComponent() {
    const [random, setRandom] = useState(0);
    const handleRand = () =&amp;gt; {
      setRandom(Math.floor(Math.random() * 100));
    };
    return &amp;lt;ReturnedComponent random={random} handleRand={handleRand} /&amp;gt;;
  }
  return newComponent;
}

export default UpdatedComponents;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We write the logic for the two previous components in a function inside a new function we can call it newComponent() with ReturnedComponent as returned value and passing the parameters needed as props .&lt;/p&gt;

&lt;p&gt;Random1.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import UpdatedComponents from "./UpdatedComponents";

type Props = {
  random: number;
  handleRand: () =&amp;gt; void;
};

const Random1 = ({ random, handleRand }: Props) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Mihed random number {random}&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; handleRand()}&amp;gt;reroll&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default UpdatedComponents(Random1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Random2.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import UpdatedComponents from "./UpdatedComponents";

type Props = {
  random: number;
  handleRand: () =&amp;gt; void;
};

const Random2 = ({ random, handleRand }: Props) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Mihed random number {random}&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; handleRand()}&amp;gt;reroll&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default UpdatedComponents(Random2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the two components shares thier logic in a higher level reducing the repetition and now Random1 and Random2 only renders the result.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
