When will be it be useful to use a high order components in react?
For further actions, you may consider blocking this person and/or reporting abuse
When will be it be useful to use a high order components in react?
For further actions, you may consider blocking this person and/or reporting abuse
Voke -
Antonio Moruno Gracia -
Victor Ogbonna -
ramadhan.dev -
Top comments (4)
They're useful for reusing logic.
For the sake of example, imagine that we have two different views for blog posts, but we get their data in the same way.
Instead of duplicating the logic for both views, we could have the logic in a higher order component and pass in whichever of the two views we wanted.
Another thing is that higher order components can be stacked. We could do
withFoo(withBar(withBlogPostData(BlogPostView1)));
and it would work just fine. ObviouslyBlogPostView1
would need to be coded correctly to use all the props it would receive.However these days we tend to use React custom hooks instead.
Obviously I'm simplifying the example by not including
useEffect
orcomponentDidMount
, but I hope the explanation still comes through.So in cases of error handling, am sure it can useful then. will try and implement it. Thank you.
I've just finished reading this post which describes HOC and some alternative approaches to reusing components.
Thank you. will check it out