Why I Love useReducer

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.ย. 2024
  • React Hooks have been out for a couple of months now. I’ve had the chance to play with them during that time and recently found an amazing example of where the useReducer Hook really shines.
    I was so excited about how much stronger useReducer made my components that I had to share it with you.
    Have a watch and let me know if you also love useReducer!
    ------
    BLOG POST: hswolff.com/bl...
    SOURCE CODE: github.com/hsw...
    PATREON: / hswolff
    TWITTER: / hswolff
    SITE: hswolff.com/

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

  • @hswolff
    @hswolff  5 ปีที่แล้ว +46

    Thanks for watching everyone! I'm glad you're as excited about useReducer as I am!
    If you like the video and want to keep supporting more of it please consider becoming a Patreon supporter! Every little bit helps! ❤️ www.patreon.com/hswolff

  • @kgreen175
    @kgreen175 4 ปีที่แล้ว +100

    This video just changed my life. I literally thought the whole reducer/dispatch usage was complicated and made no sense. YOU ARE A KUNG FOO TEACHER! This makes things much more DECLARATIVE and not so IMPERATIVE!

    • @hswolff
      @hswolff  4 ปีที่แล้ว +4

      Hahahahahah thank you! So glad it clicked for you!

    • @JohnCamden
      @JohnCamden 4 ปีที่แล้ว +7

      Totally agree. I was just about to dismiss reducer/dispatch as unnecessary, and I am so glad that I came across this video. Subscribe!

    • @electroheadfx
      @electroheadfx 3 ปีที่แล้ว

      I totaly agree :))

  • @jaany
    @jaany 4 ปีที่แล้ว +33

    Probably the best video I've seen on this. You're a great teacher and explain stuff well! Thanks Harry :)

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

      Wonderful! So lovely to hear, thank you!

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

    Okay this is best explanation of useReducer I've found on internet.

  • @driijko
    @driijko 4 ปีที่แล้ว +3

    Distinguishing between the user experience and the underlying mechanics necessary for that experience made the use of useReducer so much more intuitive to me, thanks :)

  • @LucaroP
    @LucaroP 3 ปีที่แล้ว +4

    I'm fairly new in React, especially React Native (I've been studying this lib for like 2 months, max) and this video is amazing, gave me a lot of ideas how to manage state properly. Your explanation was on point, I understood everything about the useReducer even being a beginner and not speaking fluent english (I'm from Brazil). Thanks for the video.

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

    The best explanation i ever heard, you explaining is like take our hand and explaining step by step 😂 so cool

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

    I keep coming back to this video because to me, it's the best tut on useReducer I have found.

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Aww, thanks so much for saying so!

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

    great tutorial, thank you for not tunnelling on the code and actually explaining the flow of things

  • @SepSol75
    @SepSol75 3 ปีที่แล้ว

    This was the best useReducer explanation I've seen on TH-cam!

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

    That's probably the best example of useReducer I have seen so far. I do agree that though longer, it definitely is more maintainable. I coded along and though imperative programming is still more natural to me, it got messy, and useReducer made so much sense. Thanks.

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

    That's a really nice way of thinking about it - it's all about what the user is trying to do, hence 'actions'. 👍🏻

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

    This was so great because you showed the "non reducer" part of this code to give context. Like the other comments, I had a nice, big, loud "A HA" moment. Really excellent job, thank you.

  • @pritulkhan1395
    @pritulkhan1395 4 ปีที่แล้ว

    Wow, this perfectly makes sense. Every tutorial out there only says "This is an alternative to useState" but never mentions why, and the practicality of useReducer. You just answered every skepticism I had about this. Much thanks, I have subscribed -- looking forward to more content!

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

      That's so great to hear! The why is so important!

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

    Thanks Harry. That is a great explanation of how useReducer works and why you would want to use it.

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

    Thanks so much for one of the clearest explanations and demos I've seen on this - you are an excellent teacher!

  • @GizmoMaltese
    @GizmoMaltese 3 ปีที่แล้ว

    Great explanation. This is essentially how state machines are implemented. And yes, state transitions can get very complicated very quickly. This is exactly what I needed.

  • @danielcranney
    @danielcranney 3 ปีที่แล้ว

    Harry, your channel is underrated! I've been struggling to get my head around useReducer for the last days, finding that I understand the standard 'counter' tutorials in the documentation, but as soon I try to use them on forms, I came unstuck. This really helped me to get a MUCH better understanding of how useReducer can be used in more complex ways with inputs/selects and other elements. Thanks so much!

  • @joelei4179
    @joelei4179 4 ปีที่แล้ว

    This is fantastic. I wrote a production feature that had 10+ useState hooks. It got messy really quick. This is exactly what I need. Thank you for the guide.

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Woohoo!! So glad it helped!

  • @michaelprenez-isbell8672
    @michaelprenez-isbell8672 2 ปีที่แล้ว

    Hey Harry - this video really helped me with useReducer. Thank you for taking the time to make it.

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

    What a great example! Thank you

  • @TheANDAMIRO7
    @TheANDAMIRO7 3 ปีที่แล้ว

    Thanks Harry for this amazing video.
    It's been more than two years since this video has been uploaded on youtube, it gives me the answer what I should say about userReducer on my job interview.

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

    dude, rarely i learn something new when I watch a react video that's actually immediately relevant and implementable. Bravo, bravo! I HATE these big huge lists of useState calls in my components. To me, all that stuff should not be in a component. It's just ugly. And this way of thinking and presenting component code in terms of user results rather than direct state is definitely the way to go. Thanks, dude. Then off of this I can imagine implementing something like a "requiresQueries" array, which then automatically fetches (or refetches) and caches user data (I use Apollo Client, gql server, etc) when certain actions are dispatched.

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Yep! So glad it clicked for you!

  • @BookOfSaints
    @BookOfSaints 5 ปีที่แล้ว +38

    12:37 and I thought I was good with keybindings. This is just witchcraft.

    • @hswolff
      @hswolff  5 ปีที่แล้ว +5

      Haha 😆 🧙‍♂️

    • @strelkan
      @strelkan 5 ปีที่แล้ว

      @@hswolff thats VS Code right? how you do that?

    • @felipealvesj
      @felipealvesj 5 ปีที่แล้ว +6

      @@strelkan Alt+ctrl to extend your cursor and ctrl+shift to select until ",". I needed to try when i saw this lol

    • @SamuelRiveraGomez
      @SamuelRiveraGomez 4 ปีที่แล้ว +6

      In Mac OS
      1. option + Shift and drag down/up to extend the cursor.
      2. option + Shift and ⬅️/ ➡️to select text.
      3. Then command + c to copy and command + v to paste. 🧙‍♀️

    • @pankajsharma-rf1ov
      @pankajsharma-rf1ov 3 ปีที่แล้ว +1

      @@felipealvesj same here and ya thnx 4 this

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

    Thanks Harry, I've watched a number of videos spending a lot of time describing how to use useReducer but making rather lame attempts at the all important "WHY" you might use it. Your thoughts on improving reasonability via declarative code really drove this home for me, far more than the usual "Use reducer when you have more than 1 state" spiel.

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

    I was doing so well and I have understand what problem the useReducer solves but I lost it when you started implementing it in code :"D

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

    legendary! a practical example from what is wrong to what is right. Please make a video on React Context with your own words and experience and how it relates to useReducer

  • @pankajsharma-rf1ov
    @pankajsharma-rf1ov 3 ปีที่แล้ว

    best tutorial for useReducer() hook on TH-cam

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

    Just loved your enthusiasm and vibe for programming! Keep rockin!!!

  • @BehruzbekOtayev
    @BehruzbekOtayev 3 ปีที่แล้ว

    Wow, thanks for showing it with useState first. That made things much more clear. I don't know if I'd understood it without it. Kudos to you.

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

      Tremendous! So glad to hear it!

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

    Nice video bro .You helped clear my confusion of why should i use reducer instead of state.

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

    Great video. Loved the practical use of useReducer

  • @JamesQQuick
    @JamesQQuick 4 ปีที่แล้ว

    Great video. Converting over some of the code in my Gatsby site to use UseReducer. Thanks!!

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Woohoo! Another happy convert!

  • @aseel1024
    @aseel1024 3 ปีที่แล้ว

    thank u, I was struggling to understand this, I got confused between useState and useReducer!
    I like the way u show how can useReducer do it better & clean way than using useState.
    this is the best way to teach

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

    that field action type is a power move

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

    Nice, really liked the video. Can't find many such insights online

  • @MarkDyck
    @MarkDyck 4 ปีที่แล้ว

    This is brilliant. I’m working on a component that I’ve made needlessly complex. Dispatching based on user behaviour is an extremely helpful mental model. Thank you!

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Woot! These are the things I love to hear! So glad it helped!

  • @racheleedwards9060
    @racheleedwards9060 3 ปีที่แล้ว

    Wow, this is magical! Definitely makes more sense how the functionality of useReducer is used from knowing Redux. This is greatly explained -- thanks for uploading!

  • @inexplicable01
    @inexplicable01 4 ปีที่แล้ว

    Love the explanation. Seems like use reducer is a way to make a bunch of state work together. It's like a mega state.

  • @zuhryfayesz-me6uy
    @zuhryfayesz-me6uy 3 ปีที่แล้ว

    Fantastically explained the useReducer() .... Brilliant !!!!!

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

      Glad it was helpful!

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

    This is awesome - your voice on top is crazy addictive :)

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

    This is one of the best explanations for useReducer that I have found! Thanks :)

  • @marinceanadrian5750
    @marinceanadrian5750 4 ปีที่แล้ว

    Thanks a lot for your time! Although I still find useReducer a complicated hook, watching your video made it less impossible to comprehend

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      I'll take it!

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

    Great explanation! Thanks a lot Harry!

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

    This is awesome and totally changed how I think about useReducer and handling form inputs. Thanks so much!

  • @ShinEduKen
    @ShinEduKen 5 ปีที่แล้ว

    This is the best useReducer explanation on YT. Thanks Harry!

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Thank you! Glad it was so helpful!

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

    Been trying to get my head around useReducer recently w/o having any Redux experience. This is super useful man, thanks!

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Woo!! Great to hear!

    • @puyanwei
      @puyanwei 5 ปีที่แล้ว

      @@hswolff Quick question, is the reducer function (in this case loginReducer) typically put in the same place as the Component the state is used? Was kinda expecting the reducer function to go in a separate folder "reducers" or something, like redux.

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

      It's better to keep it in the same file as the component as that's the only place where it's going to be used. IMO easier to read if it's in the same file. If you want to share it (which is unlikely with useReducer, as it encourages it to be component level) then you could put it into its own file.

    • @perc-ai
      @perc-ai 5 ปีที่แล้ว +1

      @@hswolff you can also make your own custom resusable hooks and dispatch them!

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

    this was a fantastic video, it had great pacing and the explanations were top notch. You made thsi very easy to understand. Definitely will be subscribing from this video.

  • @darrenlester7410
    @darrenlester7410 5 ปีที่แล้ว

    Excellent demo Harry, really made the benefits of useReducer clear to me. Thanks!

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      You are welcome! Glad you enjoyed!

  • @ashoksabal5486
    @ashoksabal5486 4 ปีที่แล้ว

    Amazing video on useReducer 👍👍👍

  • @fluntimes
    @fluntimes 5 ปีที่แล้ว

    Wow, great explanation of the hook and a really detailed but quick run through the thought process of developing this UI. Subbed!

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

    Example was spot on :)

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

    Thanks for the video dude, I combined what I learned from this video and the "Advanced Hooks" video to create an app that serves a login page and a todolist with both being managed by useReducer. Pretty cool!

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Woo! That' s so great to hear! Congrats!

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

    Great video Harry. Thanks

  • @ГенаПетров-н5ы
    @ГенаПетров-н5ы 3 ปีที่แล้ว +1

    You can use one useState with actions (functions for state changing) and get the same result, but without boilerplate and with simpler types (if use TS)

  • @antoniott15
    @antoniott15 3 ปีที่แล้ว

    Love the way that u change from useState to useReducer, really good video!

    • @hswolff
      @hswolff  3 ปีที่แล้ว

      Thanks!

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

    you are awesome. have a great experience to watch it

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      So glad you liked it! Thank you!

  • @munirmahmud1415
    @munirmahmud1415 3 ปีที่แล้ว

    Awesome explanation with a great example. I really learned a lot about useReducer hook.

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

    amazing explanation, best teacher

  • @temzeks
    @temzeks 3 ปีที่แล้ว

    am i missing something, or i have smashed my head upon the screen trying to understand useReducer for the past three days, but this video makes it look so easy? TF?

  • @hunterjameso6096
    @hunterjameso6096 4 ปีที่แล้ว

    This was great! Nice short and concise

  • @Samuels691
    @Samuels691 3 ปีที่แล้ว

    This is the best explanation! Subscribed

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

    Awesome explination! Thanks for the content!

  • @AHRienks
    @AHRienks 5 ปีที่แล้ว +4

    Very good tutor, really! There is one issue though: `isLoading: false` should be added to the 'logout' case; otherwise you cannot log in again without a browser refresh.

    • @hswolff
      @hswolff  5 ปีที่แล้ว +3

      Good catch!

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

    Very comprehensive demonstration of the useReducer hook's usefulness, delivered fluently. Thank you.
    {2021-10-28}

  • @mjonausk
    @mjonausk 3 ปีที่แล้ว

    Good work. Thanks for contributing to peoples learning.

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

      :)

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

    This is a great explanation. Thanks

  • @Powerful-Manifestor-
    @Powerful-Manifestor- 2 ปีที่แล้ว

    Hey! Great video. I am new to React. 2 questions:
    1. Why do we write reducers outside the component?
    2. Why not have separate function doing whatever needs to be done for each event, combining all useState instead of using useReducer?

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

    This is immensely useful. Thank you :)

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

    Great video, just at the right time when I needed it

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Awesome! Glad to hear!

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

    It also means when you've got lots of child components that need to interact with the parent component you can just pass the reducer and dispatch as props, instead of having to manage lots of different sate in the props.

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

    You're an incredible teacher! Thank you so much!!

  • @learnbit_bo
    @learnbit_bo 3 ปีที่แล้ว

    genius! great example now I can understand the reason of using useReducer thanks a lot for this great video

  • @voyageruk2002
    @voyageruk2002 4 ปีที่แล้ว

    Great video, and really well explained. Thank you for taking the time to make this.

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

    Man your cool!!! You presented it simpler. I wonder where were you 🤔when I was learning it from scratch.. keep up the good work.
    By the way subscribed👍

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

    Excellent video!

  • @MarkJKellett
    @MarkJKellett 4 ปีที่แล้ว

    The best thing about redux, is the shared application state for the entire application, and also the middlewares which offer system wide side effects such as persistence to localstorage among other things.
    Redux is so much boilerplate however and in most cases, I find that alot of state stored in reducers is more suitable to component level state (form entry and validation!!!!)
    It is a useful tool however!

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      100% agreed!

  • @szeredaiakos
    @szeredaiakos 3 ปีที่แล้ว

    My mom is rolling her eyes. She used to use punch cards. They where neatly organized into functions.... Now people have emotional attachment to them.
    ...Same people who say functional programming is the future. XD

  • @Gabriel-cd5jx
    @Gabriel-cd5jx 3 ปีที่แล้ว

    Very good tutorial. Thanks!

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

    Really Great video one of the best explanation of reducer that I have ever seen but, can u tell me how u did the VS trick to copy all the state names at 12:40

  • @brunofilgueiras3518
    @brunofilgueiras3518 3 ปีที่แล้ว

    just fell in love with this hook too hahaha. Thanks, Harry.

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

    Great. Nice walk through and explaination!

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Thank you!

  • @EliasGuderian
    @EliasGuderian 3 ปีที่แล้ว

    Best explanation that I have heard so far, thank you very much. subbed

  • @base4our
    @base4our 3 ปีที่แล้ว

    Great explanation!

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

    This video is AWESOME. Really useful!! Thank you : )

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

    thanks for the gem man . This videos is so helpful .

  • @joefads5040
    @joefads5040 4 ปีที่แล้ว

    I love this! Its so easy to get stuck in the mindset of the different states and interactions inside your component, when you bring it back to the user and the interactions they will take... it seems to really simplify things and prove the nicety of the useReducer hook! I recently used the useReducer hook for the first time with some complex component state (mesauring the widths of secondary navigation tabs... then adding them together... to see if the total width of all them fit inside some parent container... if there was no room then add them to a more dropdown!). If that makes sense?! Anyway long winded story on my use case for it!! :) Also got to play around with useLayoutEffect too!

  • @kimeiga
    @kimeiga 4 ปีที่แล้ว +3

    this is absolutely amazing. React took some time but now it looks like its closer to the mental model we all have while coding a component: what we want the user to be able to DO. With this in mind, which global state package (i.e. Redux) would be the best one to use with React now to mesh with this new hook api?

  • @xbsidesx
    @xbsidesx 4 ปีที่แล้ว

    Obrigado for the explanation, I think I'm gently "stealing" some of your concepts to bring more examples of how to handle it for juniors here in Brazil, if you so agree.

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Haha feel free! Glad to see the knowledge spread!

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

    Very helpful, even after 3 years

  • @Rolanditou
    @Rolanditou 4 ปีที่แล้ว

    Damn!! Finally someone explained this in a way I 100% understood!!.... Thank You!! Liked & Subscribed! Can't wait for more from you, great content, example was perfect! really Good stuff

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Welcome! Thank you!

  • @liamlababidi8311
    @liamlababidi8311 3 ปีที่แล้ว

    That's so cool, I'm thinking of useReducer the same as Redux, GoodBye Redux!

  • @rtorcato
    @rtorcato 5 ปีที่แล้ว +4

    For people bashing redux. Redux uses React context. You should think of redux as context on steroids. Redux dev tools are so good.

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

      Absolutely! Great to point out!

    • @rtorcato
      @rtorcato 5 ปีที่แล้ว +4

      And dan abranov created redux and he is on the react team now. That’s why usereducer looks so familiar to redux users

    • @creative-commons-videos
      @creative-commons-videos 5 ปีที่แล้ว +1

      @@rtorcato bringing baby to home :)

  • @austinhall5880
    @austinhall5880 3 ปีที่แล้ว

    Great video, you made it easy to follow! Thank you!

  • @aregbesolajohn5281
    @aregbesolajohn5281 5 ปีที่แล้ว

    thank you Harry, pretty awesome and clear tutorial...... great time to be using hooks!

    • @hswolff
      @hswolff  5 ปีที่แล้ว

      Yes, I agree! Glad it helped!

  • @bantinggamer4962
    @bantinggamer4962 4 ปีที่แล้ว

    im totally going to try it tomorrow, thanks for the video!

    • @hswolff
      @hswolff  4 ปีที่แล้ว

      Thanks for watching! I hope the trial goes well!

  • @devagr
    @devagr 4 ปีที่แล้ว

    Pretty good explaination! Though one small issue I'd say is the way you handled the metafields stuff (how you change the username and password through dispatcher). You are allowing anyone who is calling the dispatcher to set random fields inside your state to random values. Not that big of an issue, but something that might be worth a small refactor.

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

    I wish I could give double likes on TH-cam. This was just too amazing. Thanks a lot

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

    You are a great teacher :)

  • @bibhushankarki9194
    @bibhushankarki9194 3 ปีที่แล้ว

    liked your explanation!