Don't Use React Context!! Use This instead

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ย. 2024

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

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

    🎉Our Newsletter is live! Join thousands of other developers
    islemmaboud.com/join-newsletter

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

    Personally, I think Zustand is a perfect client-side state management library

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

      Zustand literally nailed client state management. End of conversation

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

      @@junsgk Agree. Zustand with well-defined slices is the best

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

      We're using for all projects at our company but this library also looks fine. It simply comes down to a personal preference.

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

      Reatom

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

      True. No headaches. Just straight to the point.

  • @airixxxx
    @airixxxx ปีที่แล้ว +31

    The only reason it's re rendering is because you put the state on the App component, that's what's re-rendering. This is not a Context problem, it is a logic problem, if you create a separate file with the context of the app and pass it to its children you wont get re-renders.

    • @NarinderSingh-dh3cq
      @NarinderSingh-dh3cq ปีที่แล้ว +7

      Yes, this is the solution we dont need to use any external library for this

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

      Yes, fully agree! I thought about this during all the video.

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

      The children of the context provider, their children, grandchildren and great grandchildren will all get re-rendered. That’s the issue and it is a context problem.

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

      Yes, you will get re-renders. Components cannot selectively consume individual pieces of a context, the most obvious solution to this is splitting up context into a bunch of different contexts.

    • @madman-uw9ii
      @madman-uw9ii 11 หลายเดือนก่อน

      If you also put a different file . It will rerender all the context consumer

  • @ronaldpaek
    @ronaldpaek ปีที่แล้ว +14

    I thought that's how context is normally supposed to work? I don't think Context actually causes rerenders but since you're declaring the state and passing that into Context anytime the state changes it will cause the whole context wrapper to re-render?

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

      Exactly

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

      This is a known issue in React core. If you look into other store libraries (e.g Redux, Zustand, Jotai...) they only subscribe and re-render the target components. Not all children!

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

      ​@@CoderOneif you create a context provider and manage it state in deferent file instead in the App component it will fix the Cart component re-render and also you can create separated contexts one for cart and second for products you can avoid CartList component rerender when cart state change or use zustand instead

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

      @@CoderOne thanks that's good to know. :)

    • @madman-uw9ii
      @madman-uw9ii 11 หลายเดือนก่อน

      ​@@YacineAhmaichbut notice that all the context consumers will rerender .hence ,therefore provider should not have more state

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

    redux-toolkit uses a Provider as well, I remember this won't have a rerender on the entire sub components, corret me if I'm wrong please.

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

    What the plugin prints before functions "Ask Easy code Explain ... "?

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

    Only issue I have with this video is you're saying "re-render" often, but that's not actually what's happening. A functional component's function being invoked doesn't mean the DOM was updated. If a function call results in the DOM being updated that's a re-render. This is just React calling the function to see if the DOM needs to be updated, which it doesn't, so the expensive part of redrawing the DOM gets skipped.
    That said, it's still extra work that's being done pointlessly. And depending on what you have going on in your components it could be a major performance hit. So it's worth keeping this in mind, and I'll take note of this context issue from now on! Thanks!

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

      I think the term got overloaded in the context of React, what you are talking about is somewhat confusingly called a re-paint while re-render is used to mean render function call in react.

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

    do you have any resource that deep-dives the last bit around putting console.log inside the useEffect vs directly in the function body - you mentioned something about concurrent rendering - does it mean that React concurrent rendering would re-execute your function but it may not actually re-render (because useEffect is not called)? but why?

  • @crim-son
    @crim-son ปีที่แล้ว +22

    Honestly, i think I'll just stick with using context the way it is, React is fast, no cap.... I'll only resolve to this if i have noticable performance issues, and not when my component is rendering just a little more than normal

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

      Most optimisation is premature. Wait until you need it.

    • @crim-son
      @crim-son ปีที่แล้ว +1

      Exactly 🙏

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

    bad explanation about why cart is being re-rendered in the "bad" way example
    you have the state in App component, if you change state there it will re-render all the components, including cart, and cart will log being re-rendered, also you're using useEffect without a dependency "[]", why not just console.log in function body directly?, you don't need useMemo here
    what you can do is create a usable function that has the state in it, so the state isnt App level, but rather another component level that returns the providers and children that it will enwrap
    if you re-render App by changing one of the states, it will re-render any component in the whole app anyway if it's not in cached with useMemo lol
    I am gonna explain again just to make sure
    if you have an App and you return the simples component you can imagine
    u use useState in App and change the state of that variable, the simplest component will re-render, that's what you're basically doing
    what I am saying to do is, to create a component that will take in children through props, you use useState inside for state management and return provider writing the children
    changing that state wont re-render the cart
    if you want to know more about that why that is, google "blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior #component-render-optimization-techniques" and search for "component-render-optimization-techniques"

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

    Why you put log with render inside useffect? It should log info being standalone console log anywhere in the body of component

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

      If you stick a bit longer till this part 11:53 you would understand why!

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

    Can you provide sandbox to try myself?

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

    Of course you will rerender the hole app, because you have two states defined in the App component, every time some of its children call setState on these products or cart, the hole app will rerender. Instead of putting all children components inside .Provider, you should only put {children} as prop in it. And this way, you will not trigger a rerenders. And second, every component that is subscribed to this context, will rerender, regardless of not using cart, but using products. This is your fault that you mixed two different contexts into one. You should create two different contexts one for the cart and another for the products, then everything will work fine without unnecessary rerenders. If you want to mix all of your logic into one place, one src of truth then State management library like Redux, because the contextAPI is not created for mixed logic

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

    I decided to try context a couple months ago for the first time on a work project and im not sure i really understood it but shoe horned it in anyway and got everything working or so i thought, now nearing the end of a project and getting all these unintended re-renders from parent state changes Q_Q no idea how to fix it..

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

    I already used it with RTK query. Make selectors. Same approach

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

    How does this relate to: "This Context API Mistake Ruins Your Whole React App (All Components Re-Render)" by ByteGrad? Doesn't that solve the issue?

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

    Why don't use simply customHook asking for the data object of the context

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

    Which vs code theme u are using @coderone please reply

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

    I saw someone using context with refs and a pup sub system. that may be the new approach for me. Using refs stopped the propagation of changes while subscribing to specific stores allowed for only the specific component to update its own state with the new info and make the change there.

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

      In my opinion it's too much pay to deal with refs and pub sub for a simple context. I would switch to Zustand or Redux. at least for the newer features!

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

      @@CoderOne yea I keep hearing about both again. Redux definitely upped their game. But I try to limit imports. Is it an issue of performance or just ease of setting up as to why I should switch?

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

    it reminds me in VueJS simply provide("key", "value") and inject("key"), it's blazingly fast 🙄

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

      Vue is underrated imo

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

    its okay, but i prefer zustand, jotai or rtk

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

    can I have the github repo?

  • @long-live-linux
    @long-live-linux ปีที่แล้ว

    Web frontend articles are filled with "use this instead"

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

    Redux ?

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

      too complicated. so many abstractions

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

      @@user-tk5wt2xu9s not really. I just learned and used it in 3 days. It just took that much time to learn react-redux and redux toolkit. Trust me they are very good. You can maintain state in small slices. Not everything in one place. Also state updates does not trigger all app like context does .

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

      I'm with team Zustand 😅

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

      @@laxmiprasanna4092 exactly, I feel like many people haven't actually tried redux toolkit.

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

    Context is not a global state manager. React describe it as another (smartest) way to pass props to children. So it's normal that every child will refresh changing params to the context.
    Thats why redux exists.

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

    thanks for this video

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

    dont use react

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

    zustand!

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

    👍🙏

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

    You Just avoid that using the pattern provider and stop using useContext like a junior dev

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

    I stopped watching after 2 minutes because I struggle to understand you. It would help if you were to articulate better or speak slower

  • @xtemecollection8540
    @xtemecollection8540 7 หลายเดือนก่อน +1

    Use this instead, use this instead.. nonsense

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

    useContext is just another useless React hook.
    I personally just use Jotai, matter of fact, most of the time I just write Jotai myself. If you've never done that yourself, you might think it's complex but it's only around 10 lines of code that you only write once throughout your app.

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

      @@icheckedavailability Did you try google?

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

      I believe both are made by the same dude, and i think he actually just recently released a 3rd state management library too

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

      do you know that Jotai uses context as well? :) github.com/pmndrs/jotai/blob/main/src/react/Provider.ts

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

      I will take a look. Usually useContext for small states is not a problem.

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

      @@najlepszyinformatyk1661 really nice answer, I thought to examinate the library and to see how they "deal" with it. You can watch the video which I posted in the comments bellow, use-selector-context is using the original context too and I think it is hard to say is it with better performance or not.

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

    So, Its better to go with redux. Subscribed btw.

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

    jotai is much better choice

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

    wrap the component with memo() ?

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

      Not gonna work. React.memo() only works if props/hooks don't change. React useContext literally passes down the parent's props to *all* children under it. So any of those props changes, both parent and all children re-render