Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does still make sense to use HOC with React Hooks? [duplicate]

I have a question:

Does still make sense to use HOC with React Hooks?

If yes, in which cases would be a good idea to use it?

Thank you.

like image 285
Alfrex92 Avatar asked Apr 12 '19 17:04

Alfrex92


People also ask

Are hoc still used in React?

HOCs are not part of the React API, per se. They are a pattern that emerges from React's compositional nature. Concretely, a higher-order component is a function that takes a component and returns a new component.

Is hoc still relevant?

In a modern React world, everyone uses function components with React Hooks. However, the concept of higher-order components (HOC) is still applicable in a modern React world, because they can be used for class components and function components.

Can we use Hoc With React Hooks?

HOC helps to isolate logic and state management in a separate class-based component. With React Hooks, state management can occur outside of a class. Hooks empower developers to use the functional programming aspects in React.

Can Hooks replace hoc?

HOC is a React independent pattern, but Hooks are part of the React. We can compose HOCs like functions but React Hooks are not composable.


1 Answers

According to the official docs, only if the HOC needs to do more than to render a single child:

Do Hooks replace render props and higher-order components?

Often, render props and higher-order components render only a single child. We think Hooks are a simpler way to serve this use case. There is still a place for both patterns (for example, a virtual scroller component might have a renderItem prop, or a visual container component might have its own DOM structure). But in most cases, Hooks will be sufficient and can help reduce nesting in your tree.

As I interpret this, react-redux connect would still be a valid HOC, though useSelector and useDispatch are available so why bother ¯\_(ツ)_/¯

like image 111
Aprillion Avatar answered Oct 18 '22 11:10

Aprillion