useCallback - In 3 minutes [ and why its different from useMemo ]

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

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

  • @jasonlough6640
    @jasonlough6640 ปีที่แล้ว +8

    This was better than most other explanations. A big confusion I see is when people think it prevents a rerender. It does not. It merely keeps a functions referential equality the same between renders, unless a dependency changes. In your example, for ex, the child component still rerenders when you hit the other button.

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

      It *can* prevent a rerender, if the child component is wrapped in React.memo (which prevents rerenders if props did not change)

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

      Yes and I think it's normal.
      In React, when a parent component re-renders, all child components also re-render by default.

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

    Best and simple explanation! Thanks!

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

    Thanks for the explanation, very clear!

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

      Glad you found it useful!

  • @Daniel-ve8yr
    @Daniel-ve8yr ปีที่แล้ว

    Nice. Straight to the point!

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

    Great explanation! Thank you!

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

    Very clear, thank you!

  • @53Strat
    @53Strat ปีที่แล้ว

    my nex question is, when would you want to pass a function as a prop. Can you not simply code in such a way to prevent that? Beginner question.

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

      Yes, you usually (maybe always) *could* structure your code in such a way in which you do not need to pass a function as a prop. But sometimes it's better.
      For example: let's say we have a component, which should pull data from an API when first rendered. Our app is a banking app with 2 lists on the UI, one for deposits and one for withdrawals.
      We *could* write two lists, one which hard-codes making an API call to get your deposits and another which makes an API call to your withdrawals. But this would violate the don't repeat yourself principle, since the UI and styling would be the same for each List component.
      Instead it's pretty clear that the better solution is to create a single component, which takes in a prop of "getData()" as a function. Then the logic within List is to call the getData() function and display the results.
      In our parent component we can now have and

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

    Thanks!!

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

    too fast. hv no idea what is going on but just flashing of cursor and highlighting.