React Hooks useMemo Tutorial

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

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

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

    Clearly spoken, well structured, to the point, no umming and arring... These videos are fantastic.

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

    Wow I have spent more than 48hrs solving a problem with re-rendering but the last 5 minutes of this video saved my ass... :) thanks Ben

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

    I start to think react might be going down a road of over complexity things. In order to do efficient rendering, u have to watch out all sorts of performance pitfall, considering react memo, usecallback, usememo along the way and pick the right dependency. This wouldn’t even be a problem if u you only change/update the value where it has been changed instead of do a freaking diff and render at any chance it get. Used to be react fan btw, now I’m thinking what’s the point and all these starts to get silly. All of a sudden, reactive programming looks charming

    • @DiegoJutsu
      @DiegoJutsu 4 ปีที่แล้ว

      You are not alone

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

    Amazing tutorial on useMomo hook. Thanks for this Ben.

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

    wow this is extremely helpful, thank you my inner coding god

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

      Dunno if anyone cares but if you guys are bored like me during the covid times you can stream all of the latest movies and series on instaflixxer. Have been watching with my gf for the last couple of months :)

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

      @Trent Miles yup, I have been watching on InstaFlixxer for months myself :)

  • @neelakshsharma4936
    @neelakshsharma4936 4 ปีที่แล้ว

    Amazing stuff. After reading the hooks docs, if I don't get what it tries to convey, I refer these videos and it pretty clearly.

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

    You are awesome, thanks for making these types of videos!

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

    I love this hooks series. Thanks for the videos !

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

    You're awesome! This is one of the best react channels on youtube

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

      Thank you!

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

    A way you can find the largest word in an array is with the Array.reduce() method, here's how I did it:
    const longestWord = ['some', 'words'].reduce((biggest, curr) => {
    return curr.length > biggest.length ? curr : biggest
    }

    • @EndiFreeKolesio
      @EndiFreeKolesio 5 ปีที่แล้ว

      It can get even easier with Array.find ;)

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

      this is not algorithm channel :P ofcourse there's different ways to do that

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

    Straight to the point. Great video Ben!

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

    Thanks for awesome contents... could you please create a video on setting monorepo with cra and typescript?

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

      sure

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

      @@bawad also please include redux

  • @nikosspiropoulos8417
    @nikosspiropoulos8417 4 ปีที่แล้ว

    great video!!! explains a lot about both useCallback AND useMemo !!!

  • @shabeebck3057
    @shabeebck3057 4 ปีที่แล้ว

    So basically useMemo is using to execute a function when a change value of its given dependency, thats amazing !

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

    absolutely brain-melting!!

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

    Great video!
    Quick Question: Isn't this same as calling computeLongestWord inside useEffect and setting longestword with useState?

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

      kind of, that would cause a rerender though

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

      @@bawad I just did a quick test, seems to behave the same for me. Am I missing something?
      codesandbox.io/s/react-hooks-usememo-example-lowde

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

      yeah they would have similar behavior but calling the setter in useEffect would cause another render

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

      @@bawad ah!.. got it.
      after I put a console.log outside computeLongestWord(), I can see the extra render without useMemo !
      Interesting.., computeLongestWord() is still only called once in both scenarios, so performance seems to be negligible.
      Thanks for helping me understand it finally !!!

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

    .json url was dead. found another copy here raw.githubusercontent.com/ajzbc/kanye.rest/quotes/quotes.json

  • @rediculiqi4136
    @rediculiqi4136 4 ปีที่แล้ว

    This was very helpful for me , please make some more of those , with optimization performance topics

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

    I'm not sure why would you specify the function itself in the use memo second parameter. In which case would that be useful? By the way, thanks for your videos, extremely useful

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

    Great channel thanks for the awesome content!

  • @markovujanic
    @markovujanic 4 ปีที่แล้ว

    Thanks Ben, this is really useful in context explanation.

  • @matijatu
    @matijatu 4 ปีที่แล้ว

    I'm wondering if there is a reason why the unmount is inside of a separate useEffect. I'm talking about useFetch @ 1:50

  • @AntiMassovka
    @AntiMassovka 4 ปีที่แล้ว

    Nice, bro. Like the way you code. Thanks for the info!

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

    Just wanted to say, i love the hooks series :) Maybe cover creating custom hooks and things like that :)

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

      Any particular custom hook you'd like to see made?

  • @trungnguyen5947
    @trungnguyen5947 4 ปีที่แล้ว

    Another way to find the longest word:
    function computeLongestWord(data) {
    if (!data) {
    return [];
    }
    const longestWord = JSON.parse(data)
    .map((sentence) => sentence.split(' '))
    .reduce((prevValue, currValue) => [...prevValue, ...currValue])
    .reduce((longest, curr) => (curr.length > longest.length ? curr : longest));
    return longestWord;
    }

  • @dhruvilpatel856
    @dhruvilpatel856 4 ปีที่แล้ว

    @Ben Awad, Can we put computeLongestWord function inside useMemo (just above we call it) instead of putting it outside of component? (The point is to avoid passing as dependency)

    • @bawad
      @bawad  4 ปีที่แล้ว

      > The point is to avoid passing as dependency
      What do you mean by that?
      Putting it outside the component would allow you to avoid passing it as a dep

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

      @@bawad what if in situations where we want to export this component, does putting the function outside of it still work?

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

      Yeah

  • @AshishRawat-zl6te
    @AshishRawat-zl6te 4 ปีที่แล้ว +1

    Really amazing work Man. Just a suggestion- It would be good if u give the link to a code sandbox or codepen.io for the code, which would be really helpful for ur subscribers.

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

    Is there any reason why you put setState in a dependency array for useEffect?

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

      because if setState changes, I want to use the new value inside useEffect
      though setState is unlikely to change

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

      @@bawad it will not change.
      "React guarantees that setState function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the useEffect or useCallback dependency list."
      reactjs.org/docs/hooks-reference.html#usestate

  • @andrewking1173
    @andrewking1173 4 ปีที่แล้ว

    Thank you for these!! Needed these for work :D

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

    This was awesome but where did “arr” come from?

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

      it's the parameter github.com/benawad/react-hooks-tutorial/blob/4a5200a39a9b06a1df9ee6afd68153a47ed847af/src/App.js#L4

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

      @@bawad lolz

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

    thanks man

  • @__greg__
    @__greg__ 4 ปีที่แล้ว

    Very helpful, thanks!

  • @Isha3006
    @Isha3006 5 ปีที่แล้ว

    Could you please show, How we could use useMemo() with async await

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

      I wouldn't, I would pick useEffect for that

  • @ivanbadyulya5562
    @ivanbadyulya5562 5 ปีที่แล้ว

    What is the extension you use for block context underline?

    • @bawad
      @bawad  5 ปีที่แล้ว

      I'm actually not sure what is causing that. Here are my settings though benawad.com/plugins

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

    how to test "ComputeLongestWord"?

  • @stevereid636
    @stevereid636 5 ปีที่แล้ว

    Eslint now complains that computeLongestWord is not a valid dependency in useMemo when employing your recommended solution of relocating the function outside?

    • @bawad
      @bawad  5 ปีที่แล้ว

      does eslint want you to do: useMemo(() => computeLongestWord(data), [data, computeLongestWord])

    • @stevereid636
      @stevereid636 5 ปีที่แล้ว

      @@bawad Not when computeLongestWord is located outside the App. It does like it being a dependency of useMemo when it's outside

    • @stevereid636
      @stevereid636 5 ปีที่แล้ว

      @@bawad Here's the message:
      function computeLongestWord(arr: any): string | any[]
      React Hook useMemo has an unnecessary dependency: 'computeLongestWord'. Either exclude it or remove the dependency array. Outer scope values like 'computeLongestWord' aren't valid dependencies because mutating them doesn't re-render the component.eslint(react-hooks/exhaustive-deps)

    • @bawad
      @bawad  5 ปีที่แล้ว

      oh, just remove computeLongestWord from the dep list
      useMemo(() => computeLongestWord(data), [data])

    • @stevereid636
      @stevereid636 5 ปีที่แล้ว

      @@bawad It's just that you say in the video that computeLongestWord should be in the dep list?

  • @alexey9796
    @alexey9796 4 ปีที่แล้ว

    Thanks, but you should return an empty string not an empty array

    • @bawad
      @bawad  4 ปีที่แล้ว

      Why

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

      Ben Awad I was texting about the computeLongestWord function. Why the positive results is a string, but if there isn’t data - it’s an array? I see types inconsistency in this case

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

      oh I see, yeah idk why I did that
      your right empty string makes more sense

  • @stealthysubie
    @stealthysubie 4 ปีที่แล้ว

    You're the best man!

  • @vipulchodankar62
    @vipulchodankar62 4 ปีที่แล้ว

    Nice.

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

  • @lardosian
    @lardosian 4 ปีที่แล้ว

    Kanye Rest lol!!!

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

    Hey mate, great material. But just slow down, chill, and explain in good harmony. I can see that you're definitely a good practitioner, but when it comes to teaching, well, it's far away from getting there.... a good example of a coding teacher can be Stephen Grider on Udemy... typing so fast and going through things so quickly doesn't make it cool, it just makes it stressful.

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

      Thanks for the feedback

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

    &