Avoid losing Reactivity in your Vue Application

Поділитися
Вставка
  • Опубліковано 29 лис 2024

КОМЕНТАРІ • 83

  • @TheAlexLichter
    @TheAlexLichter  9 місяців тому +58

    Like if you ever "lost" reactivity!

  • @Kingside88
    @Kingside88 9 місяців тому +24

    I really like your videos because they are close to real life problems.
    Another big topic is avoiding losing reactivity with arrays.

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому +4

      Thank you so much 🙏
      Good point. Will write this down for another video! 👌

  • @davealex123
    @davealex123 7 місяців тому +1

    The timing of this video is crazy. I was literally wrapping a prop in a ref and was wondering why my code was not behaving correctly. This video explanation was really helpful. Thank you for creating such an informative short video.

    • @TheAlexLichter
      @TheAlexLichter  7 місяців тому

      That is indeed great timing 👏🏻
      Glad it helped!

  • @alimaher1
    @alimaher1 9 місяців тому +1

    When the video came out, I watched and said to myself 'meh, it's a rare case'. Here coming back after spending more than an hour debugging my code. What a life haha! Thanks, Alexander!

    • @TheAlexLichter
      @TheAlexLichter  8 місяців тому

      Oh wow! Sometimes coincidence is too real 😆
      Glad it helped 👏🏻

  • @BricBracASMR
    @BricBracASMR 9 місяців тому

    I'm French.
    Your content is so qualitative!
    Thank you so much!

    • @BricBracASMR
      @BricBracASMR 9 місяців тому

      @unseriousdev oui oui baguette 😋

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому +1

      Thank you for tuning in! Gald it helps 🙌✨

  • @ribl1000
    @ribl1000 9 місяців тому +1

    high value videos -some of the best most consice vue/nuxt content out there

  • @gabrielserejo8379
    @gabrielserejo8379 9 місяців тому +3

    Thanks Alex, please keep it up! I see every single video

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому

      Definitely will! Thanks for tuning in every time 🙏

  • @LiranTal
    @LiranTal 9 місяців тому

    Thanks for explaining this very simply, Alexander. I especially liked all the different options because you've hit the different cases I personally used. Please keep up with this type of content. I appreciate it a lot!

  • @cheaterman49
    @cheaterman49 6 місяців тому

    Wow, excellent video, some of this stuff can be really confusing and isn't (AFAIK) really explained in the docs either - at least not as a whole. It's also great that you make recommendations "in passing" such as "don't destructure props" - addressing common mistakes before they happen is a great idea :-). Really a must-watch video IMHO, thanks for making it :-)

  • @alibahrami6810
    @alibahrami6810 9 місяців тому

    I must say, awesome content! keep going great, Alexander!

  • @jass__0
    @jass__0 9 місяців тому +1

    After working on react for 2 years , now moving to vue and it just amazed me. But now react making huge change by bringing its compiler hope that it wont hurt vue. Xd :) Btw, Good content.

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому

      Glad to hear that the switch to Vue was fun to you 👍🏻
      Thank you ☺️

  • @mattfletcher7321
    @mattfletcher7321 9 місяців тому +1

    Your tutorials are very good! Would you be able to create a tutorial for writing a composable that could accept a primitive, ref, or getter? Or at least a deep dive into how vueuse or the nuxt composables handle this scenario?

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому

      Thank you! Yes, absolutely. Noted down 👌

  • @virusblitz
    @virusblitz 9 місяців тому

    Thanks covering this, love it when you show common pitfalls! Even with vue experience I keep making some of these :)

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому

      You are welcome! Glad it is helpful 🙌

  • @mattickx
    @mattickx 9 місяців тому +3

    Is there any overhead if we would use toRefs() compared of toRef() ?
    I do prefer using toRefs() and destructing that result. But I wonder if there is overhead due to it parsing all props instead just the ones you want to make reactive.
    Or is this negligible?

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому +3

      Nah, toRefs is fine as long as you don’t have optional values. Then, restructuring might not work as expected 😊

    • @prishanf
      @prishanf 6 місяців тому +1

      Hey Alex thanks a lot the great video. I’m still confused about what is the difference between props.title vs ()=> props.title

  • @alexandros-markovits
    @alexandros-markovits 9 місяців тому

    Nice tutorial!
    Also caution for times that objects are being passed in a component, the reactivity mechanism can end up being based on value by reference. More examples and best practices of such cases could be interesting.

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому +1

      Good point! It is general JS behavior but can still shoot you in the foot 🙈

  • @Hassanbinsafdar-jt6uj
    @Hassanbinsafdar-jt6uj 2 місяці тому +1

    wowowow bro you are a gem its solved alot of issues of my vue app :) where were you thanks for the tip

    • @TheAlexLichter
      @TheAlexLichter  2 місяці тому +1

      Glad you found this channel 🙏🏻

  • @reneeschke
    @reneeschke 9 місяців тому +1

    I didn't know props is "reactive()" under the hood, nice! I'm surprised you didn't mention making a computed of title in the component. What's your stand here? It works, but probably not recommended?

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому

      Yeah, `computed` would be my go-to solution here as props are read-only anyway!

    • @reneeschke
      @reneeschke 9 місяців тому

      @@TheAlexLichter why didnt u include the computed workflow in the video, then? just curious.
      you mean props are read only, right? refs can be changed? or am I crazy now 😂

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому

      Good question. Should've shown it too as an option, even though computed has a slight overhead (because it adds a dependency check)
      Yup, you are right - Updated my comment 😂

  • @DeepStreamBits
    @DeepStreamBits 9 місяців тому +3

    Would a computed() value work the same as toRef()?

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому +2

      Yes, absolutely! And that'd also be my "go to" version when dealing with props or `reactive` values - especially as props are read-only anyway 👌

  • @nines_99
    @nines_99 9 місяців тому +2

    probably i should read docs, but the obvious question is: what’s the difference between computed and toRef? why didn’t you use computed? and instead used toRef?

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому +3

      Computed would be my go-to for props.
      When having other reactives, toRef allows to sync between the values in both ways!

    • @nines_99
      @nines_99 9 місяців тому

      @@TheAlexLichter ooh, i didn’t know that, thank you!

  • @vuenice
    @vuenice 9 місяців тому +1

    Which title will be considered in template, props.titile or toRef(...)

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому

      The more explicit one (so const title > props.title) in the template

  • @top10acousticshowslist77
    @top10acousticshowslist77 8 місяців тому +2

    nice information not many people talk about this simple but important concept.♥♥♥

  • @youhan96
    @youhan96 9 місяців тому

    Great explanation Alex! Made complete sense!

  • @KennethFabo
    @KennethFabo 6 днів тому

    Hey Alex thanks a lot the great video. I’m still confused about what is the difference between props.title vs ()=> props.title

    • @TheAlexLichter
      @TheAlexLichter  6 днів тому

      Thanks! If you want to watch a value from an reactive object (and props are „reactive“), you need to use that „reactive getter“ pattern so the watcher knows when a value updates

  • @valacshiro378
    @valacshiro378 9 місяців тому +1

    Thabk you this solved my problem.
    Realle good videos, keep it going this ir helpful in many ways
    😊😊

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому

      Awesome!
      Happy to hear they help 🙏

  • @Like_a_lion_979
    @Like_a_lion_979 9 місяців тому

    Thank for the video, really useful. I wonder is there a way to use somehow toRefs to not lose reactivity?

    • @JiveOff
      @JiveOff 9 місяців тому +1

      From experience, toRefs can be used with the output of defineProps, which then would make all props reactive if you use the output of toRefs.

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому +1

      You are welcome!
      With toRefs, you won't lose the reactivity (as shown) ✔

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому +1

      toRef is useful for *optional* props (which are not covered via toRefs if they don't exist)!

  • @Gaijin101
    @Gaijin101 8 місяців тому

    Wonder if we ever gonna get a simpler ref vs reactive | useFetch vs $fetch.
    Its not very intuitive for most beginners.

    • @TheAlexLichter
      @TheAlexLichter  8 місяців тому

      Great idea Damian! Will put it on the list 👌

  • @LujzaPoroknovcova
    @LujzaPoroknovcova 2 місяці тому

    Nice video as always but the example with title passed as props and then mutated thanks to toRef is not the best as props shouldn't be mutated.😊

    • @TheAlexLichter
      @TheAlexLichter  2 місяці тому

      Yes, that's true! Also mentioned that in the comments before. On the other hand, a `computed` is less performant (+ there is the readonly helper)

  • @ngetichishmael
    @ngetichishmael 9 місяців тому

    This solves a lot of my problems 😂.

  • @hassankadhim5465
    @hassankadhim5465 9 місяців тому +1

    🔥🔥

  • @karshten9218
    @karshten9218 7 місяців тому

    Thanks a lot

  • @yunusemrefersiz2848
    @yunusemrefersiz2848 5 місяців тому

    An Other great video ty Alex

  • @freekeys
    @freekeys 5 місяців тому

    Exactly.... We shouldn't destructuring the props 💯, i used to do in my initial days🤦😂

    • @TheAlexLichter
      @TheAlexLichter  5 місяців тому

      And you will be able to again in Vue 3.5 with the (in 3.4 experimental) props destructuring feature 👀

    • @freekeys
      @freekeys 5 місяців тому

      @@TheAlexLichter so props destructuring won't lose the reactivity in vue 3.5 feature?🤔

    • @TheAlexLichter
      @TheAlexLichter  5 місяців тому

      @freekeys yup! Check out the RFC @ github.com/vuejs/rfcs/discussions/502

    • @freekeys
      @freekeys 5 місяців тому

      @@TheAlexLichter so pinia would also do it, current i need to use storeToRef in pinia. 🤦😂

    • @TheAlexLichter
      @TheAlexLichter  5 місяців тому

      I still would go for storeToRefs because you get refs, not „reactive“

  • @ricko13
    @ricko13 9 місяців тому +4

    the .value thing is so annoying sometimes :/

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому

      I actually think it is useful
      When using TS the IDE should autocomplete it 🙌🏻
      And then you know if the value you deal with is reactive or not 👌🏻

  • @kirayamato6128
    @kirayamato6128 9 місяців тому

    auto import is not quite good. other devs will have a headache debugging or reading the code.

    • @TheAlexLichter
      @TheAlexLichter  9 місяців тому +2

      I'd argue that it is the opposite. For me, importing ref/computed/... all the time even though it is obvious that it comes from Vue creates more noise in the code. And because TS intellisense + "go to" still works, I can easily check where things come from if needed.
      But you can also make it work without auto imports if you prefer it 😊

  • @杨磊-t6s
    @杨磊-t6s 6 місяців тому

    I think you're left-handed 5:10

  • @mrleblanc
    @mrleblanc 9 днів тому

    Intro is too loud