Тёмный
No video :(

When to Use Ref vs. Reactive // Vue Tips 

LearnVue
Подписаться 46 тыс.
Просмотров 43 тыс.
50% 1

The Vue 3 Composition API gives us 2 ways to create reactive data - ref and reactive. But which is better? Is there a difference? Find out!
✅ FREE VUE 3 CHEATSHEET WITH ESSENTIAL CODE SNIPPETS
learnvue.co/vu...
follow me on twitter:
/ mattmaribojoc
🚨 Like quick Vue lessons like these? Check out 800+ lessons over on Vue School - go.learnvue.co...
Music by Lukrembo

Опубликовано:

 

28 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 53   
@Ma1ne2
@Ma1ne2 2 года назад
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 2 года назад
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 2 года назад
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 года назад
In my view, why use ref if you can use directly reactive? Its one less step, less compute
@DEVDerr
@DEVDerr 2 года назад
@@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
@UrbanJackJr
@UrbanJackJr 2 года назад
@@DEVDerr Well, working with Proxies imo is a lot clearer than with an unknown object. Reactive is much more clear, you pass an object to reactive, and you receive and object, but with reactive data. No weird middleware objects created.
@Microphunktv-jb3kj
@Microphunktv-jb3kj Год назад
@@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 😓
@Gnarrrl
@Gnarrrl 2 года назад
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.
@martarozmi4233
@martarozmi4233 Месяц назад
Exactly the same usage
@niner8275
@niner8275 13 дней назад
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.
@Murmeltier
@Murmeltier 2 года назад
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 года назад
just made a video on this ;) but awesome point
@metalsadman
@metalsadman Год назад
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 Месяц назад
​@@Murmeltier what's your opinion on Vue, Svelte after a year?
@aleclloydprobert6897
@aleclloydprobert6897 2 года назад
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 года назад
good point, definitely right now, im using ref but who knows with future versions when patterns will change!
@ulasbirsozu4940
@ulasbirsozu4940 2 года назад
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 года назад
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 2 года назад
What a nice and clear description. the examples help me understand the difference, thanks.
@LearnVue
@LearnVue 2 года назад
thank you!
@AnotherGameDev2411
@AnotherGameDev2411 2 года назад
I recently discovered your channel. I find it VERY informative and well paced Thank you
@JerryNoel-d6i
@JerryNoel-d6i 23 дня назад
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)
@barbieroalex
@barbieroalex 2 года назад
if ref and reactive are the same, why split it?, probably vuejs team have a reason why
@JohnSmithhh
@JohnSmithhh 9 месяцев назад
Thanks! I loose so much time because of missunderstanding this concept of reactivity!
@alitokmakc1678
@alitokmakc1678 2 года назад
I prefer to use reactive for objects and ref for others type of variables
@yabuking84
@yabuking84 Год назад
I thought it was only me who always uses ref because having .value makes it easier to read that it is a reactive object. 😁
@mufashadesu7261
@mufashadesu7261 8 месяцев назад
How to reactive method and have argument like method in options api?
@jingzheshan
@jingzheshan 2 года назад
Same here, I use ref everywhere to have consistent code
@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?
@lequanglong4985
@lequanglong4985 2 года назад
Hello sir, how do you watch an ref object changing in vue3
@p.rasskazov
@p.rasskazov Год назад
Thanks!
@Markus-iq4sm
@Markus-iq4sm Год назад
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
@yadusolparterre
@yadusolparterre 2 года назад
So what's the difference between the two? Their name?
@sherpa-e5i
@sherpa-e5i 2 года назад
which VSCode color theme is this?
@vornamenachname906
@vornamenachname906 10 месяцев назад
Its only semantic sugar for saving the "value" detour.
@juanalbertoboada
@juanalbertoboada 2 года назад
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!
@LearnVue
@LearnVue 2 года назад
happy to help!
@brandonleichty
@brandonleichty 2 года назад
Very timely! I feel like this has been hot topic recently
@LearnVue
@LearnVue 2 года назад
Definitely! Glad you think so
@andreasblondeau1682
@andreasblondeau1682 2 года назад
Same here. I tend to use refs 100% of the time, for the exact same reasons you mentioned!
@ricko13
@ricko13 2 года назад
I was told not to use ref() within v-for loops... what would be the reason?
@LearnVue
@LearnVue 2 года назад
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
@AngelHdzMultimedia
@AngelHdzMultimedia 2 года назад
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" .
@trandaison
@trandaison 11 месяцев назад
I and my team use ref 100%, basically, when you see .value, you will known that variable is a ref instanlly. Faster reading speed.
@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?
@djpacu
@djpacu 2 года назад
i use ref all the time
@gaelcalloch3200
@gaelcalloch3200 Год назад
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.
@shinobi77
@shinobi77 Год назад
I use "ref() " because it's shorter 😄
@Gaijin101
@Gaijin101 2 года назад
vue is a lil borked. go nuxt or react
@sujitjoshi1240
@sujitjoshi1240 9 месяцев назад
No wonder web dev is a mess.
Далее
Кого из блогеров узнали?
00:10
Просмотров 567 тыс.
You're Probably Using Nuxt Wrong
5:16
Просмотров 21 тыс.
Pinia Simplified
4:28
Просмотров 121 тыс.
3 Ways to Persist Pinia State
4:54
Просмотров 53 тыс.
why im NOT hyped for nuxt 4
4:00
Просмотров 16 тыс.