When to Use Ref vs. Reactive // Vue Tips

Поделиться
HTML-код
  • Опубликовано: 14 янв 2025

Комментарии • 61

  • @Ma1ne2
    @Ma1ne2 3 года назад +27

    100% aligned with you, I am also only using refs, I find them much more handy than reactive. Until today I didn`t really understand why we have both, after you said "Now you are wondering, when would I use which?" I was expecting different situations where reactive is more useful than ref, but was surprised you`re handling it the same way by just using refs 😂👍🏼

    • @LearnVue
      @LearnVue  3 года назад +3

      from what ive seen from most developers, the pattern of only using ref is pretty prevalent across the Vue community right now so we’re not alone!

  • @DEVDerr
    @DEVDerr 3 года назад +40

    But generally yeah, agree. If ref() uses reactive() for objects under the hood, there is no need to use both of them. Just use ref() all the time and that's all :)

    • @xilass
      @xilass 2 года назад +2

      In my view, why use ref if you can use directly reactive? Its one less step, less compute

    • @DEVDerr
      @DEVDerr 2 года назад +6

      @@xilass Because it requires objects to pass. And you will not always want to pass objects, but sometimes primitives as well. ref() gives you this flexibility to pass primitives and objects. reactive() not

    • @Microphunktv-jb3kj
      @Microphunktv-jb3kj Год назад +3

      @@xilass javascript is not used for speed... thinking about speed and premature optimization is meaningless...
      i laugh everytime i see someone trying to convince everyone to use fastify instead of express...
      those milliseconds u shave off doesnt give you anything, the speed and performance comes from database, not the api lol... node is made for scaling horizontally...
      so it makes no sense for anyone to re-write their infra from express to fastify or something else for miniscule or almost non-existent gains...
      ref is nice and short , .. well vue macros is a new thing ; ]
      more interesting stuff, not sure if its shown in this channel...
      // ✅ won't lose reactivity with destructuring
      const { foo, bar } = definePropsRefs()
      // ⬇ Ref
      console.log(foo.value, bar.value)

    • @xilass
      @xilass Год назад

      @@Microphunktv-jb3kj man, i fully forgot what is ref and reactive since then 😓

    • @xilass
      @xilass Год назад

      Sometime ill back to see vue/nuxt scenario

  • @Gnarrrl
    @Gnarrrl 2 года назад +12

    ref() when I want to replace (.value = ) the whole object later, and/or the structure is not fixed. reactive() when the object never gets replaced as a whole, its structure is fixed, and only members get accessed.

  • @Murmeltier
    @Murmeltier 2 года назад +15

    Vue introduced reactivity transform to get rid of the necessity to write ".value" all the time. $ref, $computed, etc. - "reactivityTransform: true" in the vue object of your vite config.

    • @LearnVue
      @LearnVue  2 года назад +2

      just made a video on this ;) but awesome point

    • @metalsadman
      @metalsadman Год назад +4

      and now it's deprecated, was experimental anyway.

    • @Murmeltier
      @Murmeltier Год назад

      @@metalsadman Which is a shame, as the code is so much more readable. I have only one bigger Vue project left, before I completely switch to Svelte. Development of Vue is just disappointing.

    • @theseangle
      @theseangle 6 месяцев назад

      ​@@Murmeltier what's your opinion on Vue, Svelte after a year?

    • @MrJloa
      @MrJloa Месяц назад

      ​​​@@Murmeltier same here. Vue3 really disappointing. Not the reactivity, but overall many bs features and inconsistency like with v-bind now accepting hidden props as event handlers, lack of .native modifier (now u don't know what does @click do) etc etc etc.
      Vue2 was far more consistent.
      We'll see if Evan does his homework and fixes those issues, if not we gonna switch to svelte too

  • @aleclloydprobert6897
    @aleclloydprobert6897 3 года назад +3

    Totally agree with your opinion. I also use ref 100% of the time for the same readability reason. Not to mention that the ide adds .value if it's properly configured!

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

      good point, definitely right now, im using ref but who knows with future versions when patterns will change!

  • @ulasbirsozu4940
    @ulasbirsozu4940 2 года назад +3

    As a team we also noticed that reactive can make unnecessary mutations indeed, let say you have basic company object ( name, phone, website etc ) and you have watcher/computed based on company reactive object. If you changed something in the reactive data, watchers and computed values triggered (even if you are not watching or computing the changed value )

    • @arslanali774
      @arslanali774 2 года назад +2

      Actuallu reactive makes all nested props reactive so if u only want to make top level props reactive u can make a shallow reactive object...

  • @pavelino17
    @pavelino17 3 года назад +2

    What a nice and clear description. the examples help me understand the difference, thanks.

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

    I recently discovered your channel.
    I find it VERY informative and well paced
    Thank you

  • @niner8275
    @niner8275 5 месяцев назад

    Because with a ref, you can reactively change the whole object, with reactive, you can only change members of the object. So a ref makes it also easy to hold dynamically loaded data for example that replace the whole object.

  • @SantiagoArizti
    @SantiagoArizti Месяц назад

    I also always use refs, but, I think you can always have a variable called "const state = reactive({...})" which also makes it very clear what data is reactive and which isn't. Then, this is also easy to replace by pinia in the future

    • @SantiagoArizti
      @SantiagoArizti Месяц назад

      issue with this approach though, is that in the templates you endup using a lot of `{{ state.myVar }}`

  • @yehannk448
    @yehannk448 Месяц назад

    sir what is your vs code theme

  • @yabuking84
    @yabuking84 Год назад +2

    I thought it was only me who always uses ref because having .value makes it easier to read that it is a reactive object. 😁

  • @insideman_id
    @insideman_id 2 месяца назад

    I use ref when i want to change/assign a new object/value later, i use reactive when only need to change it's property, because when we assign a new object to a reactive variable it becomes unreactive. In fact reactive should be constant.

  • @p.rasskazov
    @p.rasskazov Год назад

    Thanks!

  • @mufashadesu7261
    @mufashadesu7261 Год назад

    How to reactive method and have argument like method in options api?

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

    I prefer to use reactive for objects and ref for others type of variables

  • @barbieroalex
    @barbieroalex 2 года назад +3

    if ref and reactive are the same, why split it?, probably vuejs team have a reason why

  • @jingzheshan
    @jingzheshan 2 года назад

    Same here, I use ref everywhere to have consistent code

  • @sherpa-e5i
    @sherpa-e5i 2 года назад

    which VSCode color theme is this?

  • @JohnSmithhh
    @JohnSmithhh Год назад

    Thanks! I loose so much time because of missunderstanding this concept of reactivity!

  • @trandaison
    @trandaison Год назад

    I and my team use ref 100%, basically, when you see .value, you will known that variable is a ref instanlly.
    Faster reading speed.

  • @JerryNoel-d6i
    @JerryNoel-d6i 5 месяцев назад

    Deep reactivity will not work if you use ref for multilevel nested objects. Simple rules are, use reactive for non primitive data and ref for primitive data (let me know if my knowledge is outdated lol)

  • @lequanglong4985
    @lequanglong4985 2 года назад

    Hello sir, how do you watch an ref object changing in vue3

  • @sarras5719
    @sarras5719 Год назад

    I always use ref, but i wrote a vue.js plugin, and data is not reactive, when I use this plugin in other applications
    if I want to update reactive state, for examle:
    const state = reactive({})
    and I want change it to:
    state = {name: ''some", age: 24}
    How can I do this?

    • @jakubschulz4353
      @jakubschulz4353 4 месяца назад

      Better late than never 😅. You can't reassign reactive. Try with:
      state.name = "some";
      state.age = 24;
      or:
      Object.assign(state, { name: "some", age: 24 });
      ...or just use ref for everything and don't worry about reactive limitations 🤷.

  • @ricko13
    @ricko13 3 года назад

    I was told not to use ref() within v-for loops... what would be the reason?

    • @LearnVue
      @LearnVue  3 года назад

      Hmmm. Is there someplace you read this? I'd love to see their reasoning. But in general, whether they're talking about refs (reactive data) or template refs, both work just fine inside v-for loops. Here's an example code snippet I made: learnvue.co/2021/06/the-beginners-guide-to-vue-template-refs-with-vue-3-updates/#using-vue-templates-refs-with-a-v-for-loop

    • @angelhdzdev
      @angelhdzdev 3 года назад +1

      If what you mean calling ref() to define a new instance of Ref inside the template, then they are right. We shouldn't do that. If what you mean using an already defined ref in the template, they are wrong.
      The difference is "instancing a Ref in the template" vs "reading a ref in the template" .

  • @gaelcalloch3200
    @gaelcalloch3200 2 года назад

    I was: "oh cool I'll find a reason to use reactive". Nope I feel relived that a lot of people don't use reactive.

  • @brandonleichty
    @brandonleichty 3 года назад

    Very timely! I feel like this has been hot topic recently

    • @LearnVue
      @LearnVue  3 года назад

      Definitely! Glad you think so

  • @vornamenachname906
    @vornamenachname906 Год назад

    Its only semantic sugar for saving the "value" detour.

  • @yadusolparterre
    @yadusolparterre 2 года назад

    So what's the difference between the two? Their name?

  • @joaquinleimeter2084
    @joaquinleimeter2084 Год назад

    I believe ref does not use reactive under the hood. There is a really nice video un Vue Mastery that explains so.
    reactive uses proxies, leveraging the get and set method to call track and trigger respectively, running effects that change the state.
    ref uses object accessors, achieving the same result using the get and set object accessors. It does not go like:
    function ref(v) {
    return reactive({value: v})
    }
    At least this is what I understood, it is true that when you console log a ref you see the word proxy, so I might be wrong.
    This is an interesting topic, does anyone have knowledge of the vue source code to explain/confirm?

  • @Markus-iq4sm
    @Markus-iq4sm Год назад +2

    So in the end, you did not clearly tell when to use ref versus reactive, saying "I use refs 100% of time" ahah it is not good reasoning

  • @andreasblondeau1682
    @andreasblondeau1682 2 года назад

    Same here. I tend to use refs 100% of the time, for the exact same reasons you mentioned!

  • @djpacu
    @djpacu 2 года назад

    i use ref all the time

  • @shinobi77
    @shinobi77 Год назад

    I use "ref() " because it's shorter 😄

  • @MrJloa
    @MrJloa Месяц назад

    General advice, never use reactive. Always use ref, ref with object inside gets wrapped with a reactive anyway. So no need for reactive. But u benefit code consistency.
    Thank me later.

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

    I use ref all the time. However, I learned a new rule or thumb rule today that could help me when working with teams.
    Thank you!

  • @Niksorus
    @Niksorus 3 месяца назад

    I could buy this argument if we had to also use .value in the template. But since we don't... That's just verbosity for no good reason.

  • @sujitjoshi1240
    @sujitjoshi1240 Год назад

    No wonder web dev is a mess.

  • @Gaijin101
    @Gaijin101 2 года назад

    vue is a lil borked. go nuxt or react