DEV Community

Discussion on: Explain Higher Order Component(HOC) in React.js like I'm five

Collapse
 
kepta profile image
Kushan Joshi • Edited

This is a very creative analogy, for a person familiar with HOCs I totally get it. I just worry it might have missed an important detail which might not be that obvious to a newcomer.

HOC is not a component but just a mere function which happens to return a component. ๐Ÿงค , ๐Ÿ‘ข would rather be the output of the HOC.
When we write โ€˜withCompX(compY)โ€™, we mean this HOC will wrap compY with CompX.

The examples below are similar in logic, the only difference is that one is done real-time and the other is done statically/by hand.

// by hand, not so cool ๐Ÿ˜Ž 
const MyComp = (
 <CompX>
  <CompY/>
</CompX>
);

const withX = (Comp) =>
   <CompX>
     <Comp/>
   </CompX>;


const MyOtherComp = withX(CompY)

// MyComp and MyOtherComp are now same with the only difference is that MyOtherComp was generated on the fly 
Collapse
 
dance2die profile image
Sung M. Kim

Thanks Kushan.

I see where my analogy breaks down as I was using each object(gloves, helmet, etc) as a concrete component, not as something that enables Tony to do something.