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

Поделиться
HTML-код
  • Опубликовано: 8 ноя 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 7 месяцев назад

      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 9 месяцев назад

    Best and simple explanation! Thanks!

  • @adrianhervasmartinez9398
    @adrianhervasmartinez9398 2 года назад +1

    Thanks for the explanation, very clear!

  • @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 11 месяцев назад

      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 Год назад

    Thanks!!

  • @sc0820
    @sc0820 Год назад +1

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