11 - Higher-Order Components in React - React Higher-Order Components with Example - React Patterns

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ส.ค. 2024
  • 👋 What is the Higher-Order Component pattern in React? A FAQ in Web Developer Interviews. Let's master this concept with examples. This video teaches you everything to get started with the React Higher-Order Component(HOC) Pattern. I hope you find it helpful 😊.
    🏃 All set? Let's go then...
    Timecodes
    0:00 - Introduction
    00:43 - What is React Higher-Order Component Pattern?
    12:40 - React Higher-Component Pattern with Examples.
    29:13 - Wrapping up & What's Next?
    🔗 Important Links mentioned in the video:
    - Source Code Used in the Project: github.com/atapas/youtube/tre...
    - Please Reach out on Twitter: / tapasadhikary
    - Showwcase ReactJS Community: www.showwcase.com/community/r...
    Other Links:
    - Blog: blog.greenroots.info/
    - GitHub: github.com/atapas
    🤝 You can support my work by following me on the above platforms and/or sponsoring me using any of these links.
    - blog.greenroots.info/sponsor
    - buymeacoffee.com/greenroots
    - paypal.me/greenrootsinfo
    Many Thanks 🙏

ความคิดเห็น • 99

  • @tapasadhikary
    @tapasadhikary  2 ปีที่แล้ว +2

    Content
    0:00 - Introduction
    00:43 - What is React Higher-Order Component Pattern?
    12:40 - React Higher-Component Pattern with Examples.
    29:13 - Wrapping up & What's Next?

  • @willianandrade1357
    @willianandrade1357 2 ปีที่แล้ว +8

    A HOC is a simple JavaScript function which accepts a component (another js function) and returns a completely new enhanced component based of the first one. The HOCs are nothing more than a way of reusing code to create cleaner and simpler React components. This was my understanding of the subject! Thank you Tapas for the amazing videos so far!

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว

      That's great. You got it right 🙌

  • @spectacular5595
    @spectacular5595 3 หลายเดือนก่อน +3

    mind Blowing , HOC is very difficult concept to understand , but you make it easy . i really thought HOC is like a rocket science but after your explanation its peace of cake . 😁

    • @tapasadhikary
      @tapasadhikary  3 หลายเดือนก่อน

      Very nice, I am so glad it helped you.

  • @practicalandplannedhomeschool
    @practicalandplannedhomeschool ปีที่แล้ว +2

    I'm relieved to finally find a video that clears up how HOCs work with the standard components! Thank you!

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว

      Great, you are welcome, Kelly.

  • @abhishekholani9579
    @abhishekholani9579 2 ปีที่แล้ว +2

    Great Content. Explanation was very good, from basic understanding to implementation with a good example. Thankyou!

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว

      I appreciate you comment. Glad it was helpful.

  • @jlvbcoop
    @jlvbcoop 2 ปีที่แล้ว +3

    Understanding what HOCs are was something I had yet to do. This video perfectly explains what they are. I have understood that with the arrival of the custom Hooks the HOC begin to be replaced, this is a great clarification. Thanks for this good explanation.

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว

      Glad to hear that 👍

    • @lindatochukwu3081
      @lindatochukwu3081 7 หลายเดือนก่อน

      @@tapasadhikary sir, does it mean both HOC and custom hooks can do the same task?

    • @tapasadhikary
      @tapasadhikary  7 หลายเดือนก่อน

      @lindatochukwu3081 yes they certainly can. When functional component came in picture, the hook usage became much higher.

  • @haroonhayat4797
    @haroonhayat4797 2 ปีที่แล้ว +1

    I see alot of videos on HOC. but Tapas Bhai you are great, clear-cut explanation 😍.
    And actually you make the web very simplified for us.

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว +1

      Thanks Bhai, means a lot. Please feel free to share in your circle for others to get it too 🙏

  • @farukabdullamunshi8358
    @farukabdullamunshi8358 2 ปีที่แล้ว +2

    Great explanation of HOC, very useful for any React-beginner to grasp the concept.

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว

      Thanks! Glad you found it helpful 💙

  • @shohanredwan
    @shohanredwan ปีที่แล้ว +1

    WoW... Just wow... Best of best... Great explanation with practical example. Thanks

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว

      Thank you! I’m glad you liked it ✨

  • @esakkis9727
    @esakkis9727 ปีที่แล้ว +1

    Really really very good videos. I seraced diffing algo and find ur video. Then addicted and watching ur react playlist from begin.
    Thank you so much for such clean and clear explanation. Kudos to your work 🎉.
    Would like to learn unit testing from your knowledge 😊

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว

      Alright! Will talk about it 🙌

  • @sonamona2010
    @sonamona2010 2 ปีที่แล้ว +1

    Brilliant way of explaining, thank you!

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว

      Thank you Uma and you are welcome 🙏

  • @eliasimokhai
    @eliasimokhai ปีที่แล้ว +1

    'Perfect' is the word for this tutorial

  • @simone-oq4if
    @simone-oq4if ปีที่แล้ว +1

    great video, the explanation was clear and simple. thank you

  • @harika218
    @harika218 10 หลายเดือนก่อน +1

    HOC is a simple JS function that takes a component and returns an enhanced component. The purpose of HOC is something that we want to reuse side effects to create enhanced components. This was my understanding about HOC. Thanks for sharing the content. Your explaining all the concepts in simple way.

    • @tapasadhikary
      @tapasadhikary  10 หลายเดือนก่อน

      Great 👍 thanks 🙏

  • @rifleking6189
    @rifleking6189 ปีที่แล้ว +1

    Great video. Thank you

  • @patshalaaa
    @patshalaaa 10 หลายเดือนก่อน +1

    Awesome ... recommend to watch

  • @sublimeelectrons8699
    @sublimeelectrons8699 11 หลายเดือนก่อน +2

    It is good to know that HOCs are just plain JavaScript function that accepts another function as its input and it returns(outputs) an enhanced function.

  • @ankitaburman5406
    @ankitaburman5406 ปีที่แล้ว +1

    Excellent Explanation Tapas 👍 🫡

  • @nikhilm5651
    @nikhilm5651 2 ปีที่แล้ว +1

    Awesome👏👌

  • @AbhishekSingh9135
    @AbhishekSingh9135 2 ปีที่แล้ว +1

    Thanks Tapas.

  • @diyaagubarah3328
    @diyaagubarah3328 2 ปีที่แล้ว +1

    Nice explanation thank you 🔥🔥.

  • @kevalpatel4895
    @kevalpatel4895 ปีที่แล้ว

    Great content and amazing explanation, sir.

  • @akhileshmishra5559
    @akhileshmishra5559 ปีที่แล้ว +1

    thank you man first time got this logic

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว +1

      You are welcome.

    • @akhileshmishra5559
      @akhileshmishra5559 ปีที่แล้ว +1

      @@tapasadhikary can you please make video on this keyword

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว

      @@akhileshmishra5559 I'll cover lots on JavaScript fundamentals including the 'this' keyword in 2023. That's my primary plan to do on TH-cam.

  • @JamiiTraffic
    @JamiiTraffic ปีที่แล้ว +1

    I have read about HOCs several times, but never clearly understood. This was well done.

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว +1

      That’s wonderful, thank you!

    • @JamiiTraffic
      @JamiiTraffic ปีที่แล้ว +1

      @@tapasadhikary I am a great fan of yours. Just messaged your react organization.

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว +1

      @@JamiiTraffic THANK YOU! It means a lot to me. Btw, which React Organization you meant? ReactPlay? Where can I find your message?

    • @JamiiTraffic
      @JamiiTraffic ปีที่แล้ว +1

      @@tapasadhikary Yes, ReactPlay. I sent it to the mail displayed on the git profile overview.

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว +1

      Thank you! We have responded.

  • @himanshujaviya6021
    @himanshujaviya6021 2 ปีที่แล้ว +1

    Finally after going through bunch of videos found this and understood Hoc in one shot 🔥🔥🔥🔥⚡⚡ master. Which hook do we use instead of Hocs

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว +1

      Thanks a lot 🙏
      Which hook is based on your use case. For example if you use HOC to make api call, you can create a hook to handle that instead

  • @ettdoorson173
    @ettdoorson173 8 หลายเดือนก่อน +1

    Wonderful.. It was/is really beneficial to me... I want to find that video to pull info from a Google Sheet where you are talking about... 😃

    • @tapasadhikary
      @tapasadhikary  8 หลายเดือนก่อน

      You are most welcome 🤗

  • @jaycodes8790
    @jaycodes8790 2 ปีที่แล้ว +1

    I subscribed your channel on the promise that you keep sharing the experience and knowlede to me...... 😛

  • @shubhamsonawane967
    @shubhamsonawane967 10 หลายเดือนก่อน

    "perfectttttttttttttttttttttttttttttttttttttttttttt" explaination is the word for your explaination.

    • @tapasadhikary
      @tapasadhikary  10 หลายเดือนก่อน

      Hey thanks man 💛

  • @govinda399
    @govinda399 2 ปีที่แล้ว +1

    Highly recommended for every beginner, because other videos and even udemy courses are just confusing on this topic. This one must see ...

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว +1

      Thanks a lot, Govindji 🙏

  • @bhagirathibehera4776
    @bhagirathibehera4776 ปีที่แล้ว +1

    good one

  • @ankurworld
    @ankurworld 2 ปีที่แล้ว +1

    Hi Tapas da,
    Excellent video.
    Simple and easy Explanation.
    Higher Order component act Like a Machine. It takes a Component as a Input and Gives Finish Product which enhances the Component. Just like a Plug-in.
    One Question : Inside HOC code there is a withFetch Function, inside it you have written a nested class component WithFetch. Is it good to write the class component outside withFetch Function. May be in a different .js file.
    My issue is why you are nesting it ??

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว

      Yes we can write it out and import too and return. Usually you write in a separate file for reusability. In our case, it was nothing such but technically you can.

  • @LernardGrigsby
    @LernardGrigsby 2 ปีที่แล้ว +1

    I love your videos. I'm confused by the naming in line 21 of MovieList.js
    My understanding was that withFetch was the HOC and is passed the Skeleton/Wrapped MovieList component, and then returns an Enhanced MovieList component. is the convention to refer to withFetch or the exported wrapped component as the HOC? Thanks again for the videos!

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว +1

      Both works.
      1. We can do
      export default withFetch(Wrapped);
      2 We can also do,
      const Enhanced = withFetch(Wrapped);
      export default Enhanced;
      Also as we are exporting as default, we can import the enhanced component with any name elsewhere. In our example, we import with MovieList only but we could hv done as MovieListEnhanced.
      Also with... is a convention people use for HOC but it is not a strict standard.
      I hope I understood your question, Lernad. If no, kindly ask again; I'll indeed clarify.
      Thanks a lot for watching the videos 💙

  • @Ilayaraja-oy1vj
    @Ilayaraja-oy1vj 10 หลายเดือนก่อน +1

    Clear understand

  • @ankansharma4897
    @ankansharma4897 ปีที่แล้ว +1

    Sir Please make a video on lifecycle method of React class-based component.
    Also I am a bit confused. what is the fundamental difference between HOC and functional component with propsWithChildren. I can achieve similiar things using any of them. Is there any specific usecases for each of them.

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว

      People hardly use class-based components now, also in interviews, we have stopped asking questions about it 😀. So I think hardly anyone would be interested in the topic. But yes, if I get more requests, I shall make it too. Thanks!
      Coming to your other questions, they are two different patterns but the end goal they are serving is quite the same.

  • @gandharvaravi4371
    @gandharvaravi4371 2 ปีที่แล้ว +1

    in APP.JS you are importing MovieList component, but inside MovieList component you are exporting HOC ? i am confused.

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว

      Hey Gandharva, as the export is a default export, you can import with any name. So while exporting we pass the MovieList component to the HOC to get a brand new component and exporting that. As we are doing default export, we can import with any name. In the video I'm using the same MovieList name. We can use any other name while importing like MovieListHOC.

    • @gandharvaravi4371
      @gandharvaravi4371 2 ปีที่แล้ว +1

      @@tapasadhikary got it. Thank you.

  • @vishaljaiswal5056
    @vishaljaiswal5056 2 ปีที่แล้ว

    how it is different from just passing the fetched data in props of the component which you are enhancing, beacuse the structure of movie list will always be same ,just the dat inside it is changing

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว

      The HOC helps take out reusable pieces. In our example, the HOC is used to take out the fetching of the movie data and enhance any component with that.
      You can do the same by repeating the fetch logic in both the components. Another way is to use hook to take out the common logic.

  • @numanbutt23
    @numanbutt23 ปีที่แล้ว +1

    Hey Tapas
    Yesterday night I was going in depth on the topic of HOC.
    The thing I realized that whatever I want to do in React, I can achieve that by all three options HOC, Context API, Redux
    So I am confuse now, where do I draw the line between them.
    And for what specific usecase, I should use them and why?

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว

      Hi Numan, thanks for asking! HOC, Context API and Redux are three different concepts serves their different purposes of application building.
      HOC: A higher-order component is a function that takes a component as input and returns a new component with additional functionality. It reuses code, logic, and state across multiple components. So Reusability is the key here.
      Context API: The context API is used to provide a global state that any component in the application can access without the need for props. It helps simplify the data flow and reduce the code needed for prop drilling. In general, you have some static value in context and use it. An application may have many contexts for managing themes, logged in user's state, etc.
      Redux: Redux is a state management concept used to manage the application state in a predictable and centralized way. It helps to simplify the data flow and make debugging easier. Usually, One global state that any part of the application can modify.
      Let me know if your understanding is better than before. Feel free to ask questions.

    • @numanbutt23
      @numanbutt23 ปีที่แล้ว +1

      @@tapasadhikary although, I already know the difference between them by definition.
      But if I see the broader picture, all of these things are doing one thing and that is separating business logic from UI of the component.
      Let's forget the concept of global state management, and if we see then HOC separates the UI and business logic, redux separate the UI and handle the business logic in the store and slices, and context API also separates the hooks logic from the actual component where they are being implemented. Obviously, the way by which they do all this changes, but each task can be achieved by all three of them at the end.
      Previously, maybe my question wasn't clear that much, but hope it makes sense now?

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว

      That’s right! This goes back to the basic programming design principles of sharing, reusability, etc. There are different ways to achieve these.
      IMO, the way we should think of each of these functionalities is about their speciality and areas of use. Underlying the concept or bigger goals could be similar.

    • @numanbutt23
      @numanbutt23 ปีที่แล้ว +1

      @@tapasadhikary now, I got your point. Thanks

  • @haseebalamrafiq9915
    @haseebalamrafiq9915 2 ปีที่แล้ว +1

    I really like your videos sir. Love from Pakistan.
    Sir, Should I return 2 components, which we have same logic, like first is wrappedComponent and other is for TabularData component so should i do that ?
    const withFetch = (WrappedComponent, TabularData) => {
    then in return
    return (

    )
    Is this possible ? I thing by doind this then we take a advantage of HOC in real means.

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว +1

      Hi Haseeb,
      Greeting from India. I'm glad you are finding the videos helpful.
      You have a great point about the HOC usage. Yes you can do that by passing multiple components. Just a small correction in the example you showed. The jsx return should wrap them in a div or fragment.

  • @andrewaghoghovwia1948
    @andrewaghoghovwia1948 ปีที่แล้ว

    Thanks for the explanation, I love it but Higher Order Components can also be used in Functional Components? Can't it?

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว +1

      Thank you!
      Yes, absolutely, you can do it.

  • @testify7228
    @testify7228 ปีที่แล้ว +1

    Please make it functional components

  • @amrbenattia
    @amrbenattia ปีที่แล้ว +1

    Good video but showing subscribe every time is annoying 🎉

    • @tapasadhikary
      @tapasadhikary  ปีที่แล้ว

      Hey thanks for letting me know

  • @talhaahmad1796
    @talhaahmad1796 2 ปีที่แล้ว +1

    You deserve 1 Million

    • @tapasadhikary
      @tapasadhikary  2 ปีที่แล้ว

      I hope, one day 😀. Thank you

  • @prathameshkhadakban1485
    @prathameshkhadakban1485 2 ปีที่แล้ว +1

    Thanks Tapas.