Why I Love useReducer

Поділитися
Вставка
  • Опубліковано 10 лют 2025
  • 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 5 років тому +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  5 років тому +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 :))

  • @sebastianteaca7794
    @sebastianteaca7794 2 роки тому +1

    that field action type is a power move

  • @MainakGhosh7
    @MainakGhosh7 3 роки тому +1

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

  • @jaany
    @jaany 5 років тому +33

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

    • @hswolff
      @hswolff  5 років тому +1

      Wonderful! So lovely to hear, thank you!

  • @little_toast
    @little_toast 3 роки тому

    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!

  • @SepSol75
    @SepSol75 3 роки тому

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

  • @anton9410
    @anton9410 2 роки тому

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

  • @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.

  • @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 :)

  • @XiagraBalls
    @XiagraBalls 2 роки тому

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

  • @LucaroP
    @LucaroP 4 роки тому +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.

  • @michaelprenez-isbell8672
    @michaelprenez-isbell8672 3 роки тому

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

  • @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!

  • @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.

  • @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!

  • @joelei4179
    @joelei4179 5 років тому

    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  5 років тому

      Woohoo!! So glad it helped!

  • @RunnerBrain
    @RunnerBrain 4 роки тому +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.

  • @CsabaKissColorado
    @CsabaKissColorado 3 роки тому +1

    What a great example! Thank you

  • @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!

  • @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.

  • @mahmodissa4169
    @mahmodissa4169 3 роки тому +1

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

  • @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!

  • @BehruzbekOtayev
    @BehruzbekOtayev 4 роки тому

    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  4 роки тому +1

      Tremendous! So glad to hear it!

  • @humtohchutiyehai1016
    @humtohchutiyehai1016 3 роки тому

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

  • @KungFuPandaria
    @KungFuPandaria 3 роки тому +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.

  • @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!

  • @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!

  • @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

  • @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!

  • @zuhryfayesz-me6uy
    @zuhryfayesz-me6uy 4 роки тому

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

    • @hswolff
      @hswolff  4 роки тому +1

      Glad it was helpful!

  • @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

  • @aseel1024
    @aseel1024 4 роки тому

    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

  • @learnbit_bo
    @learnbit_bo 3 роки тому

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

  • @karllu8664
    @karllu8664 2 роки тому

    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.

  • @carefree_ladka
    @carefree_ladka 3 роки тому

    Great video. Loved the practical use of useReducer

  • @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

  • @antoniott15
    @antoniott15 4 роки тому

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

  • @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!

  • @pankajsharma-rf1ov
    @pankajsharma-rf1ov 3 роки тому

    best tutorial for useReducer() hook on UA-cam

  • @Pareshbpatel
    @Pareshbpatel 3 роки тому

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

  • @racheleedwards9060
    @racheleedwards9060 4 роки тому

    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!

  • @munirmahmud1415
    @munirmahmud1415 4 роки тому

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

  • @tusharrebaca
    @tusharrebaca 3 роки тому +1

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

  • @EliasGuderian
    @EliasGuderian 4 роки тому

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

  • @fredjudymom
    @fredjudymom 4 роки тому +1

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

  • @avi7278
    @avi7278 5 років тому +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  5 років тому

      Yep! So glad it clicked for you!

  • @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.

  • @nmeibergen
    @nmeibergen 3 роки тому

    Great explanation! Thanks a lot Harry!

  • @pranav854
    @pranav854 3 роки тому

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

  • @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 5 років тому +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

  • @gadayash9
    @gadayash9 2 роки тому

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

  • @Powerful-Manifestor-
    @Powerful-Manifestor- 3 роки тому

    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?

  • @Rolanditou
    @Rolanditou 5 років тому

    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  5 років тому

      Welcome! Thank you!

  • @mormeshulam9580
    @mormeshulam9580 5 років тому +1

    you are awesome. have a great experience to watch it

    • @hswolff
      @hswolff  5 років тому

      So glad you liked it! Thank you!

  • @mikeyo5154
    @mikeyo5154 2 роки тому

    Great video Harry. Thanks

  • @mjonausk
    @mjonausk 4 роки тому

    Good work. Thanks for contributing to peoples learning.

  • @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!

  • @ashoksabal5486
    @ashoksabal5486 4 роки тому

    Amazing video on useReducer 👍👍👍

  • @reyanrahman
    @reyanrahman 4 роки тому +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

  • @Samuels691
    @Samuels691 3 роки тому

    This is the best explanation! Subscribed

  • @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!

  • @DaRkOSeRrAnO1
    @DaRkOSeRrAnO1 4 роки тому +1

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

  • @farizbey
    @farizbey 2 роки тому

    This is a great explanation. Thanks

  • @nitsanbh
    @nitsanbh 2 роки тому

    Example was spot on :)

  • @seungju_hong01
    @seungju_hong01 4 роки тому

    Thank you for awesome video
    your videos are much helpful and make me motivated to study React

  • @vaylx2253
    @vaylx2253 3 роки тому

    This is immensely useful. Thank you :)

  • @robinkhilery8478
    @robinkhilery8478 2 роки тому

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

  • @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!

  • @brunofilgueiras3518
    @brunofilgueiras3518 3 роки тому

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

  • @voyageruk2002
    @voyageruk2002 4 роки тому

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

  • @bkawk
    @bkawk 5 років тому +1

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

    • @hswolff
      @hswolff  5 років тому

      Awesome! Glad to hear!

  • @masruralam6199
    @masruralam6199 2 роки тому

    amazing explanation, best teacher

  • @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.

  • @ГенаПетров-н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)

  • @hunterjameso6096
    @hunterjameso6096 4 роки тому

    This was great! Nice short and concise

  • @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!

  • @albert21994
    @albert21994 4 роки тому +1

    Awesome explination! Thanks for the content!

  • @salemouail627
    @salemouail627 4 роки тому

    Hey even if this for beginners , i enjoyed watching this.
    U got a nice soul

  • @liamlababidi8311
    @liamlababidi8311 3 роки тому

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

  • @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.

  • @vincenguyendev
    @vincenguyendev 5 років тому

    Thank you for making such a really clear content! keep up a good work bro

    • @hswolff
      @hswolff  5 років тому

      Thanks for watching!

  • @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!

  • @abedjarjanazy1178
    @abedjarjanazy1178 3 роки тому

    thanks a lot, it was a very clear a nice explanation

  • @maor44
    @maor44 3 роки тому

    Thank you for that.

  • @MarkJKellett
    @MarkJKellett 5 років тому

    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  5 років тому

      100% agreed!

  • @yogevstrauber8657
    @yogevstrauber8657 4 роки тому

    Thank you Harry !
    Great video !!!

  • @PelFox
    @PelFox 4 роки тому +1

    This is so clean and well explained. Thank you! Im a backend developer trying to dip my toes into React.
    I just managed to clean up my contextProvider from having five different states and setters to just use a reducer.
    This also made my code elsewhere much more clean as well :)
    I can definitely see the gains start to kick in when you are working with nested structures rather than simple inputs.
    Right now I put my action, reducer and context in same file. Is this wise or is it better to put the reducer in a separate file, like people did with Redux? I'm using Typescript so I have type safety for my actions.

  • @bibhushankarki9194
    @bibhushankarki9194 3 роки тому

    liked your explanation!

  • @temzeks
    @temzeks 4 роки тому

    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?

  • @insaneviruss
    @insaneviruss 5 років тому +1

    Great. Nice walk through and explaination!

  • @AveN7ers
    @AveN7ers 2 роки тому

    What's the shortcut for moving cursor to the end/beginning of a word?

  • @brothercaleb
    @brothercaleb 4 роки тому

    Wow. I’m convinced.

  • @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!

  • @base4our
    @base4our 3 роки тому

    Great explanation!

  • @skyescrawford9774
    @skyescrawford9774 3 роки тому

    I found this video is really helpful, so I leave my comment here, thank you so much

    • @hswolff
      @hswolff  3 роки тому

      Wonderful! Glad you did!