Context API Tutorial For Beginners - Using React Hooks

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ต.ค. 2024
  • In this video I will teach you guys how to use the Context API for State Management in React. We will be using the CreateContext and UseContext hooks to create a context allowing us to manage different states in a React Application.
    -
    🚀 Learn ReactJS By Building 6 Projects: codedamn.com/l...
    Please leave a comment on what topic you guys want me to cover next!
    -
    📞 Tutoring Session: www.fiverr.com...
    💻 PedroTech Discord: / discord
    -
    Social
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    Website: machadopedro.com
    Twitter: / pedrotech_
    Linkedin: / machadop1407
    Instagram: / _pedro.machado_
    Github: github.com/mac...
    Email: machadop1407@gmail.com
    Tags:
    Context API
    UseContext
    ReactJS Tutorial
    ReactJS and MySQL
    NodeJS Tutorial
    API Tutorial

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

  • @stackercoding2054
    @stackercoding2054 3 ปีที่แล้ว +73

    Thanks for creating this video straight to the point, I was able to implement context in my project in just a few minutes.

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

      Glad you liked it :)

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

      @@PedroTechnologies Thanks bro you saved my time your method of explaining is fantastic

  • @apawar3099
    @apawar3099 3 ปีที่แล้ว +11

    Thanks "SIR", This was the best explanation I found after searching all day....Thank you so much Pedro❤️

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

    This is absolutely, beyond a shadow of a doubt, the best explanation of this on TH-cam.
    Thank you for helping me understand it completely in 10 minutes.

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

    Why do you need showProfile or setShowProfile when you are setting a username? If a username exists then that means you are logged in

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

    For once I finally understood and implemented context API...you explain so easily and straight to the point, with points to take. Thank you

  • @abhcode
    @abhcode 27 วันที่ผ่านมา +1

    On point thanks man!!
    To sum up :
    1. create a context.js for a particular group of children who want to access (or share) same states
    2. import that context in parent of those grouped components (by grouped components i mean the children who are going to share same states without "prop digging")
    3. provide values for that context which can be accessed by the children
    4. To access in children destructure the object passed in provider and use useContext(contextName) to get the object passed in the provider
    whoooo this ends the summary that i have learnt from this video!!😊

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

    to all the different siblings ☺

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

    Good video though when I refresh the page it will not work. What I expect is to see profile after I login and refresh. More information required for this video to be useful

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

      In react when you refresh the page all the states will reset. If you want to maintain the information after refresh it has nothing to do with the context api, but rather you have to save data into the local storage or cookies :)

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

    you just saved me 1 hour of my life in 10minutes

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

    I struggled so much to find someone explaining it with another example than just the theme example, and very clearly.. THANKS DUDE !!

    • @VP-qd2ed
      @VP-qd2ed 5 หลายเดือนก่อน

      damn mee too cant understand the white boys shit

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

    Isnt it simpler to just pass those via props ? Much easier ?

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

      Hey, you can pass states through props but that is very limited. Also, with the context api you don't need to pass each global state as a prop to every single component. I believe context api is much easier to work with!

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

      @@PedroTechnologies isnt redux doing the same thing ?

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

    a little bit confusing I don't know why after all I have done I still have a blank page

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

    Great tutorial! Getting straight to the point was super helpful in understanding it better and easier.

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

    Good explanation of how to use the Context API!
    Beginners should know that this may not be the best example for when you should utilize Context. Since this example requires lifting up the username and showProfile states only one level, it would be more straightforward to pass the necessary state and setState functions from the App component level to the Profile and Login components as props than to use context.
    I enjoyed the content nonetheless. Thanks Pedro.

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

    Bro you need to speak slow. you mix up alot of things in between, making us even more confused.

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

    best explanation , simplest , clearest, absolutely best. Thank you so much !

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

    Thanks, that was really helpful! I have a question though, can we create a context in a child component and pass it to other sibling components?

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

      Thank you! You can't do that, all context states must be created on the parent component and wrapped around the components you cant to have access to that state.

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

      Use redux management

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

    best explanation!! thank you !

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

    Thanks for this clear explanation! I still feel there should be easier way to execute such a simple feature... I'm quite a noob with full stack/ web dev and don't know much about these but I'm questioning the value of learning React. It's not very intuitive and it seems to be quite complex and hard to learn, is it really worth putting in all the hours to learn to use it?

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

    I got my job watching your tutorials ❤️ Thanks bro

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

    Thanks, it's very helpful! can we get data from Axios and showing listing through context API. any video link would be very helpful...

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

      You can create a context state and set its value equal to the result of the axios request

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

    I do not know why you are so underrated , You content is amazing , to the point like it should be

  • @fares.abuali
    @fares.abuali 2 ปีที่แล้ว +3

    Thanks for creating this video straight to the point. Such a great tutorial 👏🏻

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

    Excellent explanation, thank you for sharing!!!

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

    Thank you a lot! A great tutorial, straight to the point, and easy to understand. I admire your simple way of explaining it. I always find it difficult to understand it.

  • @pavanilla4374
    @pavanilla4374 3 ปีที่แล้ว +12

    useContext +useReducer =🧨🧨

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

    why do we use this ? we can also just pass them as prop

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

      This is meant to organize your project. For example, in a large project you might have several components inside of each other, therefore you would have to pass a prop inside of each of those components.

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

    Why do you have 5K Changed files?????

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

      Loll, it started with my first video lik 4 years ago. I didn't think it would matter so i didn't set up my git repo properly. Now its a running joke on the channel

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

    Another wonderful video..just to the point..no nonsense blabla! Really appreciated!

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

    Thank you very much for this tutorial. You simplified it to my best understaniding.

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

    Nice, then what is the difference between sharing the state with the context API and sending the state as props to the childs?

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

    thank u so much bro i understand working concept of context api in 10 minutes

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

    And another question, when would it be convenient using Context and when using Redux? Thank you so much!

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

      I would say context api is good for simple / intermediate projects and smaller component groups in big projects

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

    Very clear explanation. Thank you for this video.

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

    good vid dude appreciate

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

    Pedro simplifies any programming concept as water what you need to do is to drink that water and quench your thirsty

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

    brother, I have watch many tutorials on usecontext api. But this one your tutorial is so simple and short. and I get it. thank you

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

    Thank you! Finally I understood Context API. I just have a question, so all states should be saved in the App.js Component?

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

      Glad you liked it! It depends, you can start your context states anywhere you want as long as it is a component and it is the component above the other components using the states in the component tree!

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

      @@PedroTechnologies Thank you!

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

    you are genius sir

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

    The fact that you left no comment unanswered made me subscribe. Thanks for making this very simple and digestible KT video.

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

      Thanks for the sub! Sometimes I get caught up with work and am unable to answer some, glad on this one I was able to answer everyone hahaha

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

    Muito bem explicado! Fiquei curioso pelo nome do seu Canal e imaginei que fosse brasileiro, entrei na sua pagina do linkedin e pude confirmar que eh do BR e que tambem mora em Vancouver assim como eu. :)

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

    To get same result with TypeScript you can define interface and add initial values:
    export interface LoginContextInterface {
    userName: string,
    setUserName: (input: string) => void
    setShowProfile: (input: boolean) => void
    }
    export const LoginContext = createContext({
    userName: '',
    setUserName: () => {},
    setShowProfile: () => {},
    });

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

      Thank you so much bro, you saved my life

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

      @@jainamshah7126 you're welcome! 👍

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

    is the value prop like by default it has to be value can we use a different name for this?

  • @sreenathk.k9772
    @sreenathk.k9772 2 ปีที่แล้ว

    Hi.. If we are using context to save authorisation datas like userName,userId,userRoles etc from the login API ..I am able to access the datas inside any component from the context state.. but if I reload the page every state data is lost... So what will be the solution to persist the data? Because I have to use userId etc in API calls in some of the pages and reload should not cause an issue..
    Also if localstorage is a solution then is it secure? Because anyone can change the storage datas manually right?

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

      You have to use the backend for this, and save your data in a database. Of course, local storage is not secure for this.

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

    please by using useContext we will no longer need down the state to children components?

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

    great explanation

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

    Super tutorial! Thank yo you very much!) Found it to be very useful! +1 to your subs)

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

    Thanks. I just don't understand that I can create this only using the props. Why do we need Context API for this? Do we use it because it is the only solution in more complicated code?

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

      I believe when there are more complexity we prefer this

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

    Thank you 🎉🎉

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

    Good video sir! this and hitest choudhary video made my concept clear

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

    great video but you should clear your 5k changes lol

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

    Great video. I’ve literally just subscribed.

  • @LaibaAhmad-i5o
    @LaibaAhmad-i5o 4 หลายเดือนก่อน

    thnks bruh! it was much needed..i only got it from you

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

    9 dislikes? the only thing I didn't understand throughout the tutorial. Good job man

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

    This is absolutely, beyond a shadow of a doubt, the best explanation of this on TH-cam.
    Thank you for helping me understand it completely in 10 minutes.

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

    Why in the hell can't other youtubers be as straight forward as you ?????????????

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

    What an amazing explanation, 💯👌

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

    Your content is really good but the only thing that you may consider upgrading is your video quality. Please use high-res screen recorders. Other than amazing job. Keep it up!!

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

    I was scaring by the name of context API before watching your video. Finally I've learned. Thanks Dear....

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

    I understand it well. TQ for this awesome video

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

    thanks, much easier to understand than redux :), gonna use it in my projects soon

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

      Glad it helped!

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

      dont know if anyone gives a shit but if you're bored like me during the covid times then you can stream all the new movies and series on InstaFlixxer. Been watching with my girlfriend recently xD

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

      @@malikgordon9212 i dont knoe if you give a shit but i just reported your comment for spam haha

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

      @Malik Gordon Yea, I have been watching on kaldrostream for months myself :)

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

      @Malik Gordon Yea, I've been watching on KaldroStream for since november myself =)

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

    I finally understood context API. Thanks!

  • @11balajir20
    @11balajir20 2 ปีที่แล้ว

    Thank u so much brother....the best explanation I got

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

    Your videos are not only extremely helpful but also very inspiring to us brazilians as you might imagine. I'm glad to discover this channel! Valeus! =)

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

    Can I know about editor font?
    I would like to install that on my machine

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

    Ah nice is similar to CascadingParameters in Blazor.

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

    Great video again, Pedro! This was explained so well!

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

    finally found a video which goes to the point directly without any unnecessary details, thanks man. I have a question: you have provided all username-setusername-setshowprofile to the children as parameter of LoginContext provider, lets think we have tens of values that need to be shared so do we have to write them all one by one as parameter ? Or is there anyother way like defining them in LoginContext.js etc. ?

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

    its cool that i knew u were brazilian too by hearing your accent. Good work, man. Proud of your content

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

    I liked the way you are teaching us. Awesome

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

    This is the best explanation on context api so far. Short, Simple, with application. Super helpful ❤

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

    Thank you I enjoyed the video, it's simple and clear ❤

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

    Nice tutorial but it can be better if the logic was implemented in LoginContext file ;)

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

      Thanks! That could be done too! I showed it being implemented in the App.js so it wouldn't be very confusing. I was scared that implementing it in the context file it would be hard to keep track of the states. But normally you ar right!

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

    What does
    {showProfile && {userName} }
    Do bro?
    Only today iam seeing this type of code...what will {showProfile} do here...Does it mean to check whether showprofile = true and then render the h1 with the username?

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

      It means that if showProfile === true then show {userName} . Otherwise, not.

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

      @@alimirhashimli2727 thanks bro

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

    really ez to understand for low brain like me 😎

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

    Do you have the github link for this code?

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

    Thanks i think context hook is better then consumer ❤️

  • @f3-faithfitnessfinance
    @f3-faithfitnessfinance 3 ปีที่แล้ว +3

    Aaah love context❤️

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

    cool, thanks man! very helpful!

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

    Thanks. it helps a lot, content is straight to the point

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

    This is really great Pedro. thank you

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

    To the point explanation. Thanks man

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

    finally i got the concept of use context , thanks for explaining that easily.

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

    Excellent clear and simple explanation man. Thanks! You got a new subscriber.

  • @HeroKiller-rh6vx
    @HeroKiller-rh6vx ปีที่แล้ว

    Great video man, watched it with breakfast and feel like i got a great understanding

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

    this video was absolute POGGERS

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

    how can I use context between more than 2 screens ???

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

    This video has nicely explained the concept.

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

    What a level of making someone understand topic of fear...... it was smooth by you.... thank you....

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

    Amazing Tutorial Bro... You Got A new Sub.. Keep the good work up

  • @GabrielMartinez-ez9ue
    @GabrielMartinez-ez9ue 2 ปีที่แล้ว

    Thank you. For this case, isn't it almost the same as prop drilling?

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

    Very good onpoint no extra time compact tutorial

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

    Best one I can understand from him well

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

    This is what I need it. Thank you so much

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

    I'm still using redux and class based component. Lol

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

      Lmaoo There is nothing wrong with using redux (i prefere Context but redux is still important), but I recommend learning Hooks and functional components!

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

      @Bryan Devsame thing how to do in redux please teach me

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

      @@PedroTechnologies please teach this same one in redux

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

      @@pavankumar4909 redux need more setup than context api. Just read redux docs and react-redux docs to set that up.

  • @dawid-bobowski
    @dawid-bobowski 2 ปีที่แล้ว

    Thank you very much for this video. You clearly described how does Context API work and thanks to you, I was able to implement it in my project!

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

    Every day you help me in different way, I recived my first task in my job that need to use context, im search in ytb, watch your video, and now I can continuous my task.

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

    Very amazing video... Stuff explained far better than those big/famous youtube channels...

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

    Thanks man, hopefully I will never have to use Redux again, btw just so that you know in our native language Hindi, "Machado" means "to rock", which kind of makes sense, because you always rock!

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

      Hahaha thats awesome! My first name "Pedro" means rock in latin as well. So my name is rock to rock

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

    such a good tutorial thank you so much!!!