aleksandrhovhannisyan.com icon indicating copy to clipboard operation
aleksandrhovhannisyan.com copied to clipboard

What Are Higher-Order Components in React?

Open AleksandrHovhannisyan opened this issue 4 years ago • 6 comments

AleksandrHovhannisyan avatar Oct 19 '20 11:10 AleksandrHovhannisyan

Hi i have a question. When you compare between wrapper component and HOC shouldn't the HOC's code shows it returns <Component> instead of <Div> ??

Other than that great write up. Really helps me to understand and differentiate between HOC, wrapper component and the usual component.

tomvuNexus avatar Oct 20 '20 07:10 tomvuNexus

@tomvuNexus Oops, you're right :) Thanks for pointing that out! Fixed.

AleksandrHovhannisyan avatar Oct 20 '20 11:10 AleksandrHovhannisyan

Don't really see the point of using HOC anymore though. I feel hooks are so much easier to both read, write, and use. 🤔

Svish avatar Nov 08 '20 01:11 Svish

@Svish Hooks are definitely much easier to work with, but you are still going to encounter HOCs in any code base that has class-based components. You can't use hooks in classes.

AleksandrHovhannisyan avatar Nov 08 '20 14:11 AleksandrHovhannisyan

That's true. But I would still kind of recommend to never write another HOC again, unless it's absolutely necessary...

Svish avatar Nov 08 '20 14:11 Svish

That's fine. This article is intended for people who are interested in understanding HOCs. While it's an older pattern, it's still relevant, and understanding why HOCs were used (or even writing one in modern React) can't possibly hurt anyone. New patterns—including hooks—emerge from old ones.

@Svish Edit: I had some time, so I updated the post to mention hooks. Thanks again for the feedback!

AleksandrHovhannisyan avatar Nov 08 '20 21:11 AleksandrHovhannisyan