Learn useContext In 13 Minutes

Поділитися
Вставка
  • Опубліковано 15 чер 2024
  • 🚨 IMPORTANT:
    Full React Course: courses.webdevsimplified.com/...
    1 Year Free Hosting: www.atlantic.net/webdevsimpli...
    Use code KYLE for an additional $50
    In this video I cover everything you need to know about the useContext hook. I go over all the main use cases for useContext as well as many common mistakes that developers make. This is part of a series of React videos where I cover all the important hooks in React.
    📚 Materials/References:
    useContext Blog Article: blog.webdevsimplified.com/202...
    React Hooks Playlist: • React Hooks
    🧠 Concepts Covered:
    - How to use hooks in React
    - How to use context values in React function components
    - How to use the useContext hook
    🌎 Find Me Here:
    My Blog: blog.webdevsimplified.com
    My Courses: courses.webdevsimplified.com
    Patreon: / webdevsimplified
    Twitter: / devsimplified
    Discord: / discord
    GitHub: github.com/WebDevSimplified
    CodePen: codepen.io/WebDevSimplified
    #ReactJs #WDS #useContext

КОМЕНТАРІ • 603

  • @dimitrioszygolanis
    @dimitrioszygolanis 6 місяців тому +75

    In order to simplify it, I think you could create an object, like const values = { darkTheme, toggle } and pass both values inside the ThemeContext.Provider, in order to avoid creating 2 providers.

    • @Xi-tler
      @Xi-tler 4 місяці тому +2

      Yeah you r right

  • @femaledeer
    @femaledeer 4 роки тому +754

    These 13 minute videos end up taking me 2 hours to go through

    • @akshbansal7715
      @akshbansal7715 3 роки тому +19

      Same here

    • @tvguideondemand
      @tvguideondemand 3 роки тому +69

      I watched Mosh's 2 hour tutorial video and it took me 4 days.

    • @cabejackson7518
      @cabejackson7518 3 роки тому +21

      lol i thought I was alone in this! I definitely set my pomodoro to watch this then do some practice problems then 25min later I was like wow I'm only like 5min into the video

    • @cabejackson7518
      @cabejackson7518 3 роки тому +5

      still learning a lot though, just slow n steady

    • @migueldomingos4570
      @migueldomingos4570 3 роки тому +10

      @@tvguideondemand mosh videos are normmaly very detailed and so it makes sense you took so long ;-)

  • @brijspy
    @brijspy 4 роки тому +6

    Thanks a lot kyle :) I have seen so many videos of useContext hook but never seen this kind of a structure which is awesome to maintain and scale. This is gold.

  • @KevinVandyTech
    @KevinVandyTech 4 роки тому +63

    After watching this video, I refactored one of my side projects, and it's so much cleaner.

  • @piyush6631
    @piyush6631 2 роки тому +191

    Took me like 2 hours and 4 breaks to understand the refactor that you did in the separate file. I also didn't knew that useContext(ThemeProvider) was just like a consumer for function based components. Your tutorials are so amazing. What a neat way to write code, keep making good videos!

    • @ashleytennyson4717
      @ashleytennyson4717 Рік тому +3

      same here .. took me loads of time to get a grasp of this 8 min of 7zip video of something which is like a whole chapter

    • @je2587
      @je2587 Рік тому

      lol same

    • @ronyfluk8396
      @ronyfluk8396 Рік тому +18

      If it took you 2 hours to understand what he did then I guess maybe the explanation isn`t that amazing after all? 🤔

    • @piyush6631
      @piyush6631 Рік тому +8

      @@ronyfluk8396 By that logic, you can learn HTML, CSS, JS, React and Authentication in just 10 hours by watching one of the BootCamp videos on youtube. Practising and implementing stuff takes much longer than watching a video on it, no matter how good the content is.

    • @ReadyBasket
      @ReadyBasket Рік тому +2

      @@ronyfluk8396 no. I thought that too but Web Dev Simplified is a great tutor. The problem is we are probably all programmers with different years of experience and understand the problem but React introduces a new way to tackle a problem that probably didn't really exist until you started using React. Most people think to switch themes, just have two css files and have some code to change the class of the body from light to dark..... and you can even use local storage to remember the users choice...
      However if you use React, that solution may introduce other problems therefore useContext has to be understood.

  • @mrdoner3451
    @mrdoner3451 3 роки тому +4

    The most informative channel I have found for web dev. Thanks again for clearing up another complicated topic so concisely.

  • @heathenfire
    @heathenfire 4 роки тому +4

    Great video. I just started learning about useReducer and useContext. So perfect timing!!

  • @varundoshi5136
    @varundoshi5136 Рік тому +2

    This channel literally has everything that I search for. Can't thank you enough for sharing your knowledge!

  • @SaurabhNative
    @SaurabhNative 3 роки тому +6

    This was another useful video from your channel. The first example of useContext in functional/class based components was easy. Second example was a but advanced but still easy to figure out and understand. Thanks for making such awesome content.

  • @smdude4
    @smdude4 Рік тому +9

    I can't believe how easy it is to understand it the way you explained it. Using your vids as a refresher (haven't done react in a while) and it's been super helpful for my job search. Not sure you're gonna read this but THANK YOU. Will take your full course

  • @k3v1n_xlr84
    @k3v1n_xlr84 3 роки тому +5

    You are one of the best at being able to articulate advanced concepts in an easy-to-digest format. Living up to your name. I don't know where I would be without your lessons, you're literally godlike.

  • @ivantorres6577
    @ivantorres6577 3 роки тому +2

    These are amazing thanks for making these, I am in a bootcamp at the moment and we are learning react. These help clear things up a ton so thank you for making these. Keep up the good work Kyle

  • @njayman
    @njayman 4 роки тому +2

    I have been waiting for this one for so long. Thanks a lot

  • @timothylander3469
    @timothylander3469 Рік тому +18

    After watching the entire video once, then duplicating the project during a second viewing, I was able to understand the concepts well enough to add additional context variables and new sibling components that can easily communicate amongst themselves. Your explanations, demos, and walk-throughs are so clear, concise, and to-the-point that with some study, the concepts really begin to sink in. Thank you!

    • @cordial
      @cordial 6 місяців тому

      etest

  • @nanonkay5669
    @nanonkay5669 4 роки тому +94

    I think you can pass in not only state but also functions in the value attribute of the provider like this:
    {children}
    ...and then just destructor the two depending on what you need in a particular component.

    • @ylmazcandelen3121
      @ylmazcandelen3121 4 роки тому +13

      yeah, I was like that's really easy why we need another provider but he finds his way cleaner I assume.

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

      which variable would you destructure? like usually for props it is const { state, function } = props. what would you use instead of props in our children components?

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

      @@wizhaa useContext(name of your context)

    • @user-mv4oh8yp1y
      @user-mv4oh8yp1y 3 роки тому

      @@wizhaa useContext(CanYouReadThat)

    • @6365bharath
      @6365bharath 2 роки тому +2

      Yeah why would you wrap it around another provider. It messes the code

  • @Davidgon100
    @Davidgon100 Рік тому +1

    Your reactjs videos are the best! You explain so well and your examples are excellent. I understood this right away after being confused by someone else's vid

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

    fantastic video! I am trying to combine this with your "useReducer" video right now for a somewhat complex bit of state management... I think a video like these combining the two concepts would be a smash hit my dude.

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

    Amazing explanation. I've been able to refactor a complex, nested code using this pattern in minutes. Thank you !

  • @TheRealAfroRick
    @TheRealAfroRick 2 роки тому +74

    I think the hardest part of this is that you are doing it with themes. If you were to simply expose an object or something and have it available in all of the children, it might be a little bit clearer. Using the theme is actually muddying up your demo.

    • @liondepierre
      @liondepierre Рік тому +2

      Agree

    • @user-ti9yn8wg6o
      @user-ti9yn8wg6o Рік тому

      Sorta agreed. Although theme here is just boolean value, but new comers need to wrap their head around this one more level

    • @jtaylor8606
      @jtaylor8606 Рік тому +5

      Absolutely, I just can't get my head around this explanation, going to need to check another vid

    • @kainthjaskaran
      @kainthjaskaran Рік тому

      @@jtaylor8606 Hey check this video out. It helped me a bit better than this
      ua-cam.com/video/hUhWtYXgg0I/v-deo.html

    • @dvirhanum9530
      @dvirhanum9530 Рік тому

      but what about updating it's value? How would you re-render the app?

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

    I had to rewatch to fully get it, but it all makes sense now. Thanks mate! As always cristal clear explanations, keep up the great work!!

  • @13has
    @13has 2 роки тому

    You're my hero at this point. I'm literally watching this while on the job.

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

    Thank you! I was working on a little webapp and totally blanked on the pattern for creating the custom hooks (near the end of the video). You're awesome man 🎊☮️

  • @LieberLois
    @LieberLois 3 роки тому +99

    Great Video about Context!
    Im just disagreeing with creating multiple Contexts for a variable and its Setter function! You can just pass both as an object as the context value :)

    • @deonvisser2480
      @deonvisser2480 2 роки тому +4

      I was also wondering why he didn't do this.

    • @talatkuyuk6556
      @talatkuyuk6556 2 роки тому +3

      In your case, you are right. But think about you use styled-component's ThemeProvider which accepts only DefaultTheme, that is why nested provider is an option for this case.

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

    This is so perfect. Exactly what I was looking for. Thank you :)

  • @tushargoyal3088
    @tushargoyal3088 Рік тому +10

    what are your thoughts on passing on the update theme function as a value to the original theme context? (instead of it being its own context provider)

  • @AlldayIshid
    @AlldayIshid 3 роки тому +243

    I love this guy so much but he's persona is what I imagine an alien would create if they were trying to build a human

    • @pim691
      @pim691 3 роки тому +17

      I've really come to like his personality and his method of explanation.

    • @arthurswanson3285
      @arthurswanson3285 3 роки тому +23

      Right. His cadence sounds like it was the outcome of a deep learning algo.

    • @scriptkeeper8243
      @scriptkeeper8243 2 роки тому +7

      Be greatful youngling, Kyles stormed Area 51, found alien tech, then set loose their clones to teach us all how to upgrade our world and get better paying jobs.

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

      Why would an alien build a human is my question

    • @dm.hol.3624
      @dm.hol.3624 2 роки тому

      are you all gays here lol

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

    Wowwwwwww! I can finally understand this. Very well explained and broken down. I like how you explain from many different angles so you really get your point across

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

    Just what I needed this Saturday morning, made my app work perfectly! ~ Thank You!

  • @programmingpython584
    @programmingpython584 Рік тому

    These hooks videos were just awesome. really easy to understand. Thank you so much

  • @chrisstucker1813
    @chrisstucker1813 Рік тому +1

    This is an absolutely fantastic tutorial 👏🏻

  • @zevspitz8925
    @zevspitz8925 Рік тому +5

    A gentle introduction to useContext and context in general. Two points:
    As others have noted, I would have a single provider for both the darkTheme and toggleTheme values.
    Also, I found it confusing that the button now has to live within the FunctionComponent. I'm not sure which is better: wrap the App component in ThemeProvider one level up, or create an intermediate component to hold the button and the FunctionComponent.

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

    Amazing, I have a class yesterday about useContext, but now, after your video, I'm closer to be able to handle it!!

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

    This is an awesome tutorial. I needed to watche 2 times but it's very clear how useContext works. Great, Kyle. Wish you all the best.

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

    Simple and nice explanation of useContext. Thanks a lot Kyle for such a videos. Keep up good work!

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

    Thanks a lot, you are an exceptional teacher and your code looks so clean!

  • @yumyum7196
    @yumyum7196 Рік тому

    I love your videos and I usually get it on the spot, but honestly this is the first time that it went straight over my head

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

    This tutorial is highly recommended for anyone who needs to understand context API concepts, great Job KYLE ✨✨✨

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

    Thank you so much! I recommend everyone to code along and examine to understand easier

  • @AndroidCyclist
    @AndroidCyclist 8 днів тому

    Thank you so much for making these videos. My job is moving to react native from native mobile development and I have found these to be very helpful and even shared the playlist with my team.

  • @lonniesmith8093
    @lonniesmith8093 Рік тому +29

    As a react beginner I'm sure this is a great explanation for someone with a little more experience. As a beginner I found this example to much. Passing a variable down such as a name or number would have been a great start IMO. I hope this is constructive criticism. You channel is amazing!

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

    there are no words to appreciate your explanation.. infinity hats off!!! God Bless!

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

    Thanks for the simple and straight forward tutorial.

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

    Thanks for the detailed explanation as always, very helpful

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

    Well explained. This is the best explanation about useContext compared to all other videos in this subject. Thanks.

  • @vasilemidrigan50
    @vasilemidrigan50 Рік тому

    Thank you, Kyle! As always, clear, concise, and direct to the subject! 💯💥👋👌

  • @rohitchatterjee2327
    @rohitchatterjee2327 3 місяці тому

    so very helpful, just the right level of detail

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

    Thanks for makes these things very easier for me. Love your content. keep it up.

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

    Yo your youtube title is telling the truth!!! hands down the best version so far !!! thank you!

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

    Thank you for the tutorial saved me a lot of time in my current project. God bless you.

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

    This is Awesome! I’m totally going to make use this tomorrow. 👍🏾

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

    Very nice. Easy to understand. Good example of context

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

    Thanks kyle, your tutorial so simple i can easily understand 😄

  • @toohee8561
    @toohee8561 Рік тому

    Look forward for your courses Kyle!

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

    Man, you are awesome. Your videos are so much helpful. God bless you.

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

    Awesome video, I like how he increase the code without loosing the complexity understanding of the context.

  • @ShinAkuma
    @ShinAkuma 5 місяців тому +1

    This was hard to understand. So I made chatGpt write everything for me. Thank you kyle.

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

    This is brilliant, thank you!

  • @alex.noriega
    @alex.noriega 2 роки тому +5

    Hey old friend!
    Your like a team mate who takes enough time to explain what we need for the sake of continue our work.
    I always go back to this channel if there's any concept or JS feature that I don't really understand right after read hundreds of articles or blog publications. Thanks in advance.

  • @danielrojasbarco6760
    @danielrojasbarco6760 Рік тому

    Amazing explanation!! Thanks a lot.

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

    I love your channel bro, keep up the good work!

  • @PatrikRasch
    @PatrikRasch Рік тому

    Good stuff, great to get a deeper understanding of stuff I learn from The Odin Project from you on here!

  • @hwj8640
    @hwj8640 Рік тому

    awesome video, after seeing some articles on internet, this video helps me understand useContext in in only 13 minutes. thanks!

  • @mizanrifat
    @mizanrifat 4 роки тому +2

    Great tutorial as usual. I have a question. Instead of creating another context if i pass toggleTheme in themeContext along with darkTheme value is there any problem??

  • @wasimnadaf11
    @wasimnadaf11 10 місяців тому

    Ver clearly explained. The concept is really drilled into my mind. thank you for this amazing video

  • @Rosalitamaria
    @Rosalitamaria Рік тому

    Thank you! helped me a lot

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

    can't thank you enough. got this to work in minutes

  • @isacoliveira6326
    @isacoliveira6326 Рік тому +4

    This is easily one of the most insightful videos about web dev I've ever seen. Not only because of the pattern it advances with useContext, but also for how well it illustrates the pursuit of encapsulation and ellegance. Kudos!

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

    Exactly what I needed dude ty

  • @kostiantynkarzhanov9216
    @kostiantynkarzhanov9216 11 місяців тому

    Very good and consise explanation! Thanks for one more awesome tutorial!

  • @rashikraj7404
    @rashikraj7404 10 місяців тому

    This was a really good tutorial!

  • @dustymccord
    @dustymccord 3 роки тому +2

    This was a great tutorial. This is something i will refer back to in the future

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

      i am refering that to myself right now, for the third time in a week

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

    Thanks Kyle for the context theme in React.

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

    I love your way of teaching.. keep it up

  • @mateusbarbosa5266
    @mateusbarbosa5266 10 місяців тому

    I love Kyle's lessons, he's probably the one instructor on youtube that I don't get bored watching, but I can't be the one who watches his videos in 0.75 speed, dude be too quick with his logical structures as he speaks lol

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

    great stuff! thanks a ton!

  • @garywaddell6309
    @garywaddell6309 3 роки тому +2

    Not too sure I understand some of the negative comments, this was a super explanation of useContext and a perfect approach to allow token based security for react apps. Thank you!

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

    I love it very neat! Thank you 🙏🏼

  • @hallowedbythyframe
    @hallowedbythyframe 4 роки тому +28

    I literally spent hours yesterday to get this to work, I have terrible timing

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

      😂 atleast you know now

  • @haidernaqvi87
    @haidernaqvi87 3 роки тому +24

    useContext: I am gonna destroy this man(Redux)'s whole career.

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

    Kyle you are the man, I can already see you as the top developer trainer in the world!

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

    wow, this is exactly how the mui built their theming. Thank you for the explanation before that I was struggling to understand it.

  • @samsonocran6347
    @samsonocran6347 Рік тому

    Thanks a lot for being my mentor

  • @DanMazzilli-pv9yl
    @DanMazzilli-pv9yl Рік тому

    Great video! Thanks :D

  • @mrluckyuncle
    @mrluckyuncle 6 місяців тому

    You're great :-) Excellent, clear explanation, and I was able to put it to use right away.

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

    This video has saved my hide! Thanks Kyle.

  • @dreamfly555
    @dreamfly555 4 роки тому +97

    This assumes you know what a context is and how in general it's being used. i.e., not for beginners.

    • @hungryreaper23
      @hungryreaper23 3 роки тому +7

      I agree, tbh this is not really a tutorial more like watching someone code while somewhat explaining his thought process.

    • @IncomingLegend
      @IncomingLegend 3 роки тому +33

      literally 90% of all tutorials assume you are a beginner and I'm getting fucking tired of it... I find intermediate or advanced tutorials such as these very refreshing and sadly they are quite rare...

    • @migueldomingos4570
      @migueldomingos4570 3 роки тому +7

      @@IncomingLegend Me too bro. Tech With Tim has done some nice intermediate and expert python playlist and also object oriented design were he doesnt treat you like a begginner. You may want to check him out

    • @number1neek
      @number1neek 3 роки тому +8

      Trust me, come back to this when you need a quick refresher and you'll love how concise this is.

  • @dazjacktar6202
    @dazjacktar6202 Рік тому

    Thank you for helping me reduce my codebase, great video!

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

    Great video. Thanks. When you 'take it a step further' around 5:45, I had to re-watch a few times. LOL. My brain shut down the first time watching. I guess, I was still processing the first 5 minutes.

  • @DeepakGupta-hj2dv
    @DeepakGupta-hj2dv 4 роки тому

    I love your channel, Keep up your work please make one video on "Event Loop"

  • @mmm29267
    @mmm29267 3 місяці тому

    great video! thanks a lot

  • @corndoggydogdog
    @corndoggydogdog Рік тому

    Love the video. Thanks for the wondeful contribution. If I wanted to use an external stylesheet in js react native, could context be applied? Not quite sure how that would work? Would love to hear from you. Thanks again for your fantastic work!

  • @marcin31337
    @marcin31337 Рік тому +2

    Can someone explain why App doesn't have access to context? If we leave the button in the App and import the same custom hooks as in the FunctionContextComponent, the application will not work. Why?

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

    This is really clear and good guide to this. Though I went through a react course and have solid fundamentals so there is a bias

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

    Great video, many thanks for sharing this.

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

    Thanks Kyle some deep explanation there

  • @AbhishekMishra-fr7po
    @AbhishekMishra-fr7po 3 роки тому

    Amazing explaination

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

    Pretty awesome video. Thanks.

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

    Wow, finally I got how it works :D
    Thank you, Kyle!
    My brain's gonna blow up. But now I should do the same alone as we all together did here :D

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

    Correct me if I'm wrong, but why do you need to create a second Context for updating? Can't you simply pass in the update function along side darkTheme in Value? I've seen multiple other videos where they do just that

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

    Thank you for this knowledgeable video.