Global State with Context in React - React context

Поделиться
HTML-код
  • Опубликовано: 28 окт 2024

Комментарии • 88

  • @Weibenfalk
    @Weibenfalk  4 года назад +5

    Hey! I noticed that I've had some unintentional reverb on my latest videos. I will remove it for later ones 😃You should be able to hear what I say anyways and no, I'm not in a big airplane hangar recording 😄

    • @FilipeRebollo
      @FilipeRebollo 4 года назад

      Man, I don't have words to say how happy I am. I went through so many RUclips videos and blogs to undertand that but until now I was not able to get anything. Thanks a lot, you are great!
      Filipe from Brazil

    • @Weibenfalk
      @Weibenfalk  4 года назад +1

      @@FilipeRebollo Thanks! Makes me happy to read that it helped you.

  • @ericyoung7820
    @ericyoung7820 4 года назад +23

    nobody else on the entire internet, including the react docs shows how to use it correctly like this. you are a boss

    • @Weibenfalk
      @Weibenfalk  4 года назад +1

      Thanks ... I'm glad to read that it helped you.

  • @ksy9364
    @ksy9364 4 года назад +4

    This tutorial saved me from tons of work for my personal project. I had a lot of components that needed to share a single state but passing it as props and handlers were a pain. Thank you!

    • @Weibenfalk
      @Weibenfalk  4 года назад

      Glad to read that it helped :) thank you.

  • @learnwithindra2037
    @learnwithindra2037 2 года назад

    Maaaan, i have been confuse with Context Concept for a few days. and now i am good with it. Many Thanks

  • @arsnakehert
    @arsnakehert 2 года назад

    Finally found a tutorial that shows how to use contexts for something that isn’t just themes, hahah
    Thank you so much!

  • @santiagosolana8741
    @santiagosolana8741 3 года назад +1

    DUDE! I cannot stress enough how helpful this tutorial was!!!! thank you!

    • @Weibenfalk
      @Weibenfalk  3 года назад

      Thanks. Glad it helped :)

  • @codeswithankit4316
    @codeswithankit4316 4 года назад +3

    Much needed video, Thanks for your help

    • @Weibenfalk
      @Weibenfalk  4 года назад

      Thanks for your support. Glad you like it! Spread the word ;)

  • @oxanasf6369
    @oxanasf6369 2 года назад

    It's a great, very straightforward video. I picked it out of others because the title explained the whole concept of Context in React in precise and clear way and made right decision.

  • @essli9430
    @essli9430 2 года назад

    You're so cool brother
    After hours of trying to understand context hook now i get like butter

  • @NeoNoggie
    @NeoNoggie 4 года назад

    That's a pretty cool component. Edit: Also, fantastic tutorial. Way clearer than all the others I have watched so far!

    • @Weibenfalk
      @Weibenfalk  4 года назад

      Thanks! Great that you like it!

  • @valdompinga
    @valdompinga 2 года назад

    man, u r saving my life, thank you so much!

  • @hassanbajwa5807
    @hassanbajwa5807 3 года назад

    You helped me a ton! So straightforward. I really appreciate it. Thank you.

  • @Triyanox
    @Triyanox 2 года назад

    Thanks a lot for putting the time and the effort to explain this concept !!!

  • @alenanik
    @alenanik 4 года назад +1

    The double await had me 🤯😅 Great tutorial! Thanks a lot!

    • @Weibenfalk
      @Weibenfalk  4 года назад

      Yeah that can be tricky ;) thanks and thanks for your support :)

  • @alexwarendh
    @alexwarendh 3 года назад

    Tack! I'm pretty new to React and have been using only useState so far. Things get complex real fast and I guessed context was the next step needed.

    • @Weibenfalk
      @Weibenfalk  3 года назад +1

      Context is a must know in React if you're going to build bigger apps =)

  • @innodjetinnodjet3121
    @innodjetinnodjet3121 3 года назад

    Salute for the tuto....very nice and easy to follow.

  • @karendoran3692
    @karendoran3692 3 года назад

    This was very nicely explained. You have really good content on your channel!

  • @thelma2084
    @thelma2084 3 года назад +1

    Better than docs, and I agree about useReducer too bulky. Yay

  • @justicemanu1644
    @justicemanu1644 4 года назад

    Nice video,I like how the flow of the work is.It makes you understand how useState with context works.

  • @usamanaeem8737
    @usamanaeem8737 2 месяца назад

    Thanks Man. It was a big help

  • @asztonselecta965
    @asztonselecta965 3 года назад

    I love you. I feel like I am the worst programmer in the world. Like really and you... you safe my life mate. Big up

  • @itachi-senpaii
    @itachi-senpaii 4 года назад +1

    Thank you so much sir . You are awesome ....

  • @yasharhasanzadeh2944
    @yasharhasanzadeh2944 4 года назад

    Thank you I was looking exactly for this tutorial. You saved me.

  • @harshprajapati2729
    @harshprajapati2729 2 года назад

    Thanks, bro Helpful for me!

  • @aljimsonmegrino9580
    @aljimsonmegrino9580 4 года назад

    Good job, you make context ez to understand, keep it up

  • @majdirajeb6091
    @majdirajeb6091 Год назад

    But the setState in the coolcomponent , does it change in the store also

  • @pandopando8996
    @pandopando8996 3 года назад

    super cool guy. great video. super clear and minimal

    • @Weibenfalk
      @Weibenfalk  3 года назад

      Hey .. thank you! I forgot to turn off the reverb on the voice on this one though =) haha

  • @aryan7069_
    @aryan7069_ 3 года назад

    which font you use please tell ?

  • @alexwheeler1386
    @alexwheeler1386 2 года назад

    Thanks for the video! I have everything set up the same except I am accessing the new state in a different component.
    I was able to console.log state in Store.js to see that it is indeed updating when I click the button 'add to cart'.
    However, when I change routes by going to the Cart component, state is then re-initialized to "cart is empty". How can I persist the state so that it doesn't reset each time?

  • @aaronsoto3823
    @aaronsoto3823 3 года назад

    When trying to pass more than one pair of state variables, Do you just add the other state, setState in the value passed to the context.provider?

    • @Weibenfalk
      @Weibenfalk  3 года назад +1

      That’s up to you to decide. No right or wrong here. You can see the context provider as a container where you can pass anything you want to be accessible in you app

    • @aaronsoto3823
      @aaronsoto3823 3 года назад

      @@Weibenfalk I got that part, what I am referring to is in the Store.js I am trying to create another state, count, setCount to pass to the provider along with state, setState. I tried to do but this did not seem to work

    • @Weibenfalk
      @Weibenfalk  3 года назад

      Hmm that should work. If you access the correct elements in the array. I would use an object instead when you have multiple ones. It’s easier to access them then by it’s properties.

  • @abrahamterfie8802
    @abrahamterfie8802 3 года назад

    thank you so much !!!!!!

  • @xxulchikxx
    @xxulchikxx 3 года назад

    Thanks for the clear explanation! I've wrapped my App component in a context provider, and I can access the global state from the App component now. However, my App component has other components inside of it, and it seems like I can't access that state inside of them. Does this only work for direct children of the context provider?

    • @Weibenfalk
      @Weibenfalk  3 года назад +1

      If you wrap the app component you should be able to access the context in the complete app. That is every child component of the app

    • @xxulchikxx
      @xxulchikxx 3 года назад

      @@Weibenfalk weird! I wonder what I'm doing wrong! I keep getting this error: Uncaught TypeError: Cannot destructure property 'openAbout' of 'Object(...)(...)' as it is undefined.
      So it thinks that the global state is undefined for some reason. It actually works when I wrap the child components in the context provider directly...It just doesn't like it when I wrap the parent component only.

    • @Weibenfalk
      @Weibenfalk  3 года назад

      @@xxulchikxx It's really hard to tell by this info. But the whole point with the context is that you should be able to wrap any component with a context and all the child components should be able to access the content. So if you wrap the App with the provider all the components from App and down should be able to access it.

    • @timothyosmond1799
      @timothyosmond1799 3 года назад

      @@xxulchikxx Im getting the same error! Did you ever find out what you were doing wrong?
      Assigning the object to my createContext worked
      export const Context = createContext([initialState]);

    • @xxulchikxx
      @xxulchikxx 3 года назад

      @@timothyosmond1799 yes, my issue was that I was using Three.js canvas, and that messes with the DOM somehow, so I had to use useContextBridge from react-three/drei to pass all that context down.

  • @planetmall2
    @planetmall2 4 года назад +1

    Great job!

  • @alulaleak
    @alulaleak 2 года назад

    Very cool.

  • @husseinsaad7969
    @husseinsaad7969 4 года назад

    Great thanks!
    what the font do you use in your code editor?

    • @Weibenfalk
      @Weibenfalk  4 года назад

      Hey thanks! That’s the infamous Operator Mono :)

  • @user-ww6ns4hn9r
    @user-ww6ns4hn9r 3 года назад

    Question: one of the problems that Redux resolve is accessing only parts of the global state instead of the global state as a whole because here you could mistakenly use setState to change the WHOLE of the global state mistakenly. Now, I know that this is not Redux, but using a Context.Consumer wrapper component around the child component, you can then extract which variables of the component you can access. Wouldn't that be better to limit the access of this component to the state?

    • @Weibenfalk
      @Weibenfalk  3 года назад

      You can have multiple context and multiple states and place them on different levels in your app. So you don’t need to put everything in one large context that wraps the complete app

  • @linus430
    @linus430 2 года назад

    When trying this with nextjs I get this: ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization

  • @santra528
    @santra528 3 года назад

    08:22 You can use word wrap (press Alt + Z) and it will bring the long line in the next line. I am sure you already know it but anyways!

  • @linus430
    @linus430 2 года назад

    Maybe a stupid question :) But If i want multiple global state variables, how would i structure that?

    • @Weibenfalk
      @Weibenfalk  2 года назад

      You can have anything in your context. For example an object with different properties. Or you can have multiple contexts and context providers

  • @ghostkee5031
    @ghostkee5031 4 года назад

    I was searching for this in the whole internet till i found your perfect example , you're great thank you.
    Just a question , is there a way to make this work if we are using CoolComponent as a class instead of a function

    • @Weibenfalk
      @Weibenfalk  4 года назад +1

      Thank you. Glad to help. You can’t use hooks inside class components.

  • @santra528
    @santra528 3 года назад

    The reason I hit the Subscribe button because of the "Coooooooooooooooooolness"!!

    • @Weibenfalk
      @Weibenfalk  3 года назад +1

      Haha thanks. Also way tooooooooo much reverb on this

  • @ejlz9542
    @ejlz9542 4 года назад +1

    Good video !

  • @centurion3708
    @centurion3708 4 года назад

    May i know what's that cool vscode theme you're using please?

  • @taraslysenko9547
    @taraslysenko9547 4 года назад

    Hello. Can you help me please? I need to create 2-3 states inside my initialState and than I want to change them in my another component. How can i do this? If this is possible of course.

    • @Weibenfalk
      @Weibenfalk  4 года назад

      Hmmm really don’t know what you try to achieve here :) why do you want to create a state inside a state? I would go for creating 3 separate states instead ... if you need them in a context you can create multiple contexts instead. Or you can have an object with multiple state properties in one state. Just as it works with this.state in a class component

    • @taraslysenko9547
      @taraslysenko9547 4 года назад

      @@Weibenfalk Thank you) I'll try it

  • @SasaRistic1979
    @SasaRistic1979 2 года назад

    Can you do a video with TypeScript??

  • @KatDog-jl3fs
    @KatDog-jl3fs 7 месяцев назад

    can you update the context from another component?

    • @Weibenfalk
      @Weibenfalk  7 месяцев назад

      You can update the context data from anywhere inside the wrapper for the context

  • @rafaelsantana4467
    @rafaelsantana4467 4 года назад

    You are a god!

  • @xXNJEEBXx
    @xXNJEEBXx Год назад

    thanks❤

  • @simpingsyndrome
    @simpingsyndrome 2 года назад

    ty cuy

  • @RainOnline
    @RainOnline 3 года назад

    i dont get it ;'(

    • @Weibenfalk
      @Weibenfalk  3 года назад

      Ok ... sorry to hear. What is it that you don't get?

  • @benediktlantsoght1706
    @benediktlantsoght1706 4 года назад

    Hidden gem

  • @Skia_
    @Skia_ 4 года назад +1

    I like your accent ;D

  • @pjguitar15
    @pjguitar15 3 года назад

    9:11 😂

  • @zafarhussain4436
    @zafarhussain4436 3 года назад

    you have not changed global state