Custom Hooks in React (Design Patterns)

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ม.ค. 2025

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

  • @cosdensolutions
    @cosdensolutions  5 หลายเดือนก่อน +1

    Hope you enjoyed the video! I also have a really cool and unique course that will teach you way more than this video. You'll learn how to build an actual complex project with React. It's called "Project React" and you can find it at cosden.solutions/project-react. Also, I have a free weekly newsletter called "Import React" with tutorials, news, and cool stuff about React! You can sign up at cosden.solutions/newsletter?s=ytc

  • @ericlorback4084
    @ericlorback4084 11 หลายเดือนก่อน +19

    These types of videos are the types of videos that I benefit from the most. Very clean and very straightforward. Thank you

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

    I love that you make it look so easy even for people who are not very much experienced with react.

  • @doorey2
    @doorey2 10 หลายเดือนก่อน +2

    Perfect tutorial. Must watch for beginner react devs

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

    very nice clear precise and effective video, every time ads come i will never skip the ads i'll try to help people like you as best as i can😁

  • @SaurabhMisra-yf8lf
    @SaurabhMisra-yf8lf 10 หลายเดือนก่อน

    Awesome tutorial! Clear, concise and exhaustive. 💯

  • @sontranvan5348
    @sontranvan5348 11 หลายเดือนก่อน +1

    it helps me a lot, really appreciate all of your videos, thank you

  • @dganiteg
    @dganiteg 4 หลายเดือนก่อน

    This video changes my entire perception and understanding of custom hooks (for the better!). Great job. Thank you so much!

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

    Amazing video. Tutorials must be like this, detailed and crisp.

  • @growingisgood
    @growingisgood 28 วันที่ผ่านมา

    Great video !! 🙏

  • @dingus1720
    @dingus1720 4 หลายเดือนก่อน

    you explain it so well. loved it, thank you!

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

    Awesome, now I need to do it again in slow motion with a lot of extra processing :)

  • @HamedAthari
    @HamedAthari 2 หลายเดือนก่อน

    you're the best man, thanks!!❤

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

    React devs, this guy is the real deal. Trust

  • @Pareshbpatel
    @Pareshbpatel 4 หลายเดือนก่อน

    React Custom Hooks so clearly explained. The tutorial also helps us understand how React-Query and React-Hook-Form do their magic. Thanks, Darius.
    {2024-09-16}, {2024-09-23}, {2025-01-06}

  • @jitujahagirdar613
    @jitujahagirdar613 4 หลายเดือนก่อน +1

    This video really helpful for me

  • @jovannijungco1761
    @jovannijungco1761 5 หลายเดือนก่อน

    Very clear. Thank you.

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

    I could learn a lot of from your videos. ❤

  • @mDHARYL
    @mDHARYL 11 หลายเดือนก่อน +1

    Nice. Must watch this later . 😊

  • @clasesutnfrc8699
    @clasesutnfrc8699 2 หลายเดือนก่อน

    04:25 Consuming custom hook
    05:33 Creating custom hook

  • @samratchandravanshi3869
    @samratchandravanshi3869 11 หลายเดือนก่อน +1

    Really quality content

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

    Great and simple tutorial to understand Custom Hooks, thank you. Just a question though - how does it improve performance comparing to the initial code as you explained?

    • @cosdensolutions
      @cosdensolutions  11 หลายเดือนก่อน +1

      It doesn't, in both cases the performance is the same. However the code is much cleaner and organized! It's not always about performance

    • @macismail2003
      @macismail2003 11 หลายเดือนก่อน +4

      @@cosdensolutions I know but you mentioned in the video. That’s what I meant. Anyways thank you.

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

    Great explanation

  • @brNoMundo123
    @brNoMundo123 11 หลายเดือนก่อน

    Seus vídeos são incríveis e explicativos 👏👏👏👏

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

    Hi I have watched your single responsibility principle, in that video you have mentioned a separate util to fetch the api, should the api fetching part in the useFetchComments should moved separately to achieve SRP ?

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

      yes that's correct! Here I left it to keep it focused on custom hooks

  • @zerosandones7547
    @zerosandones7547 6 หลายเดือนก่อน

    Hello, I'm curious why we added a refetch property name at 10:35.

    • @happylittlesynth
      @happylittlesynth 4 วันที่ผ่านมา

      It's representing the handleFetchComments function

  • @bharadwaz2
    @bharadwaz2 11 หลายเดือนก่อน

    I humbly suggest considering a tutorial series that encompasses all the essential topics.

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

      Building an entire course with that and so much more! Stay tuned ☺️

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

    can you please make a video about casl in react ? or virtualization in react (from scratch)

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

    In Initial code, it was locally adding the comment, in Your refactor code you are fetching all records again. And its a good practice to do update the state locally without fetching on each comment. So how can we locally update the state using mentioned custom hooks approach,
    Thank you

    • @cosdensolutions
      @cosdensolutions  11 หลายเดือนก่อน

      yes it was, in that case you'd need to pass the function to set the comments instead of a refetch function. The rest would work the same!

  • @jorgecarlos6277
    @jorgecarlos6277 9 วันที่ผ่านมา

    What would be the difference between useHook e useContext? Setting up they're very different, but function-wise they seem very similar

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

    Hey.. amazing explanations. A question came up... couldn't the handleFetchComments function be wrapped in a context function and be shared between the the page and the hook, instead of wrapping it with useCallback?

  • @a7lavinraj863
    @a7lavinraj863 11 หลายเดือนก่อน

    this is exactly what i need

  • @aarondean1872
    @aarondean1872 11 หลายเดือนก่อน

    Firstly, this is incredible content! Very clear instructions. Quick question: what if you've abstracted state that needs to be updated via some sort of interaction (e.g. like a button click)? do you also pass down "setters" for the state ,or is that an anti-pattern?

    • @cosdensolutions
      @cosdensolutions  11 หลายเดือนก่อน +1

      You return a function from the hook that you pass to your button to call

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

      thank you! itd be awsome if u made a TH-cam Short with an example. appreciate your content!

  • @kaluczadzsi
    @kaluczadzsi 11 หลายเดือนก่อน +1

    Love your videos, will you create a react course with TS? I would definetly buy it!

    • @cosdensolutions
      @cosdensolutions  11 หลายเดือนก่อน

      I am creating a course where you build a real-world project while being guided step-by-step. It goes much more indepth than any of my videos, and it shows you how everything fits together in a real project. I will be JS but trust me, it'll still be worth it! Coming out very soon :D

    • @kaluczadzsi
      @kaluczadzsi 11 หลายเดือนก่อน

      @@cosdensolutions Thanks! I am really looking forward to it! :)

  • @DellmantHD
    @DellmantHD 11 หลายเดือนก่อน +1

    Can you make a video guide for Vim in vs code. how to do add it and set it up. Would be very usefull. Thanks!

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

    Just a reminder: custom hooks do not need to have .tsx or .jsx file extensions, when they dont return any JSX code.
    The only thing is that they need to be called within a functional component

  • @yoky0429
    @yoky0429 11 หลายเดือนก่อน +1

    thanks you!!! (especially subtitle)

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

      you're welcome! All my videos will have subtitles from now on :D

  • @Ram-ku7me
    @Ram-ku7me 6 หลายเดือนก่อน

    Why to choose a network call to fetch comments instead of just setting it up ?

  • @anonysmooth648
    @anonysmooth648 11 หลายเดือนก่อน +1

    hi question when to use this clean up pattern?
    useEffect( ()=>{
    var mounted = true
    // do logic here
    return () => { mounted = false }
    },[])

    • @cosdensolutions
      @cosdensolutions  11 หลายเดือนก่อน +1

      I never use it tbh!

    • @Thrackerzod11
      @Thrackerzod11 11 หลายเดือนก่อน +1

      It's just something you wanna do on component unmount. Unsubscribing from things to prevent memory leaks is one of the most frequent use cases: removeEventListener, mutationObserver.disconnect() and such. But it could be anything really, you may want to console.log('unmounting') and this would be the place to do it

  • @joelbrighton2819
    @joelbrighton2819 11 หลายเดือนก่อน

    I had a discussion just yesterday about what should be contained within a custom hook. Should it only be code which includes state management / jsx or can it also include 'helper' functions (which could just be part of a non-hook helper file)? I'd be interested in hearing peoples perspectives.

    • @cosdensolutions
      @cosdensolutions  11 หลายเดือนก่อน

      I would put the helper functions in another file usually. Unless they are specifically used for the custom hook and only for that custom hook

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

    Loved the video! Would it also have been reasonable to keep the comments/setComments in CommentsPage and pass comments/setComments to both useFetchComments and useAddComment?

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

      nope, that would defeat the entire purpose of the custom hooks. If you're passing comments to useFetchComments, you also have to fetch them outside and manage that whole thing. Which puts you right where we started this video

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

      @@cosdensolutions Apologies for my React noob-ness and I appreciate the patience in advance. But, if the point of using custom hooks is to distribute the responsibility to different hooks, then is it not reasonable to have the custom hook, useFetchComments, be responsible for fetching comments while CommentsPage can be responsible for maintaining the data structure (comments) that will be shared between its two custom hooks? Also, why would I need to fetch them outside of the useFetchComments if I passed the setComment function handler as input to the custom hook? Maybe I am misunderstanding the definition of a "hook" in React. Thanks!

  • @mahadihassan5596
    @mahadihassan5596 11 หลายเดือนก่อน

    Do we have any code repo for this video?

  • @ananthac6472
    @ananthac6472 9 หลายเดือนก่อน

    so this is an alternative for HOC , right?

  • @GHSB7462
    @GHSB7462 2 หลายเดือนก่อน

    Thank you

  • @UFO_808
    @UFO_808 11 หลายเดือนก่อน +1

    Bro how long have you been coding for?

  • @powerofpavan7710
    @powerofpavan7710 5 หลายเดือนก่อน

    why to refetch it we can show the response after adding it

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

    Great video

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

    what theme are you using in vscode?

  • @siddhartha-555
    @siddhartha-555 11 หลายเดือนก่อน

    I prefer writing my custom hook code in my component file as it is better organized and easily maintainable. Is there any downside to this approach in the long run?

    • @cosdensolutions
      @cosdensolutions  11 หลายเดือนก่อน +1

      custom hooks are meant to be shared and re-usable, so they shouldn't go in any component file, but inside of the hooks folder so any component can use them

  • @meylis_so2
    @meylis_so2 6 หลายเดือนก่อน

    sometimes subtitles dont work or can not get in time

  • @ddorabang
    @ddorabang 11 หลายเดือนก่อน

    1440p. great!

  • @mladenorsolic370
    @mladenorsolic370 11 หลายเดือนก่อน +1

    When you say custom hooks have to be prefixed with use, just for clarity, its just a best practice, react doesnt impose this nor it would treat a hook function any different if it wasnt prefixed with use.

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

    i know this is not react related but, can u do a tutorial on neovim or vim or just the vim motion in your vscode

    • @mladenorsolic370
      @mladenorsolic370 11 หลายเดือนก่อน +1

      Why in the world would he care about vim? It's 2024, you shouldnt either

    • @joelbrighton2819
      @joelbrighton2819 11 หลายเดือนก่อน +1

      @@mladenorsolic370 I think some people a just masochists: "hey look at me, I can stick pins in my eyes"! :)

  • @srdjancoric2879
    @srdjancoric2879 2 หลายเดือนก่อน

    The last example adds an unnecessary network request (fetch comments) after the comment is added on the server instead of just updating the state. I am saying unnecessary since if you didn't have to use the refetch function, you would simply update the state when you receive the added comment as the response. Network requests are expensive so I would argue this is not the best practice.

  • @randompointlessness2766
    @randompointlessness2766 6 หลายเดือนก่อน

    I think you could've split the logic into simple separate components just as well 😅

  • @tanvirakon-y5j
    @tanvirakon-y5j 3 หลายเดือนก่อน

    great video. but please sync the video with the audio. this will give us pleasant experience. thanks again. learned a lot

  • @ShivaprasadBilgundi
    @ShivaprasadBilgundi 11 หลายเดือนก่อน

    a video on your vs code setup

  • @faizanshaikh2351
    @faizanshaikh2351 11 หลายเดือนก่อน +15

    Sir use the simple react without Typescript more people will watch, understand and grasp the code properly.

    • @sigilosidad
      @sigilosidad 11 หลายเดือนก่อน +48

      No thank 😂

    • @johndevnoza4223
      @johndevnoza4223 11 หลายเดือนก่อน +7

      Trust me he knows what is he doing. While ago i had same thoughts but not anynore. Learn the very basics of ts. U have to learn it anyway , early or late.

    • @jeromesnail
      @jeromesnail 11 หลายเดือนก่อน +33

      It's 2024, no one should learn React without TypeScript.

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

      people who say that often struggle with typescript thts why they complain

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

      No