Stop Using .value with ref In Vue 3! Reactivity Transformed Explained!

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ส.ค. 2024
  • #vuejs #vue3
    In vue.js there are ref and reactive. Then there are .value and the new reactivity transformed. In this video I explain in this Tutorial how reactivity transform works, how you can use ref and reactive in Vue.js!
    👉Check out my last video on ChatGPT
    • Can ChatGPT Write Bett...
    👉 Sign up and get free Vue cheat sheets and updates!
    www.vuecourse.tech
    👉 Need some help with a project, level up your skills, Vuetify or Nuxt bug? Check out my 1-on-1 mentoring!'
    mentorcruise.com/mentor/erikh...
    Links:
    vuejs.org/guide/extras/reacti...
    vuejs.org/guide/extras/reacti...
    0:00 Introduction
    0:32 How to use ref
    03:22 $ref explained reactivity transform
    04:50 reactive explained
    05:56 Using reactive with composables
    08:50 using toRefs
    09:09 using $()
    09:32 using ref with composables
    13:26 conclusion reactive vs ref what to use

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

  • @jonasex3001
    @jonasex3001 ปีที่แล้ว +109

    I don't mind about the .value at all. If you sort of understand why that's like that and the proxy object, I'd avoid extra magic.

    • @khaldounal-nuaimi3594
      @khaldounal-nuaimi3594 ปีที่แล้ว +15

      This! Many developers complain online about the “Vue magic” that breaks their libraries. We need to make Vue more streamlined and easier to integrate with libraries rather than introduce new “ magic “ that solves no problems ( imo ). Is the developer experience really that bad if developers have to type .value every time they use ref?

    • @jonasex3001
      @jonasex3001 ปีที่แล้ว +8

      @@khaldounal-nuaimi3594 the thing is that currently the magic is applied on the template, and not on the script, which makes it feel a bit inconsistent...

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

      @@khaldounal-nuaimi3594 I think I would prefer all magic, or just no magic by default (too late now).

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

      @@jonasex3001 exactly. The real problem is the inconsistent behavior.

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

      I agree with you all. I’m still using the “all magic” options API

  • @todpale
    @todpale ปีที่แล้ว +18

    I think, the simplest way to use a reactivity is just using ref as you've shown in the second example. Yes, you need to deal with .value, but in fact it's not a big problem. It just depends on the preferences.

  • @drewbird87
    @drewbird87 ปีที่แล้ว +17

    I learned Vue and Vue 3 for the first time in early 2022 and these reactivity functions were some of the most confusing things to me. I think because it uses JS proxies under the hood; the Vue team wasn't able to get around this syntax. I looked into these macros too and decided that, in most cases, they add more complexity than they're worth. Except perhaps for some library writing ( probably a headache for something like VueUse ).
    Thank you for the explainer!

    • @moon.trance
      @moon.trance ปีที่แล้ว +5

      +1 here. This dollar sign mess is awful. However, I don't have any problems with .value and reactivity in common, it makes things obvious, you can clearly understand if the variable is reactive or not.

  • @nthdesign
    @nthdesign ปีที่แล้ว +14

    If you are writing a complex app, the Composition API allows you to keep concerns like “data” and “methods” close together, and promotes code reuse through composable functions (“use” functions). That’s all very cool, and very useful! But…
    For the majority of simple components I’ve written, the Options API is just easier. Plus, for new developers, the Options API’s rigid structure provides, well, *structure*!
    I like that we can use both the Composition and Options APIs. They each have their use cases.

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

      Yes, simple components, stick to Options API.

  • @kuti1643
    @kuti1643 ปีที่แล้ว +8

    I never had a problem with adding the .value to manipulate the ref's value. These compile time macros look cool, but they add some magic that doesn't read as well as just using the API of ref, especially the $$ syntax is super weird for me.

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

    Vue3 got less intuitive than Vue2 with these .values. I can see why it went the way that it did, but it lost some of Vue2's magic. Thanks for this video, it helped clarify some things and showed some nice tricks.

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

      Exactly! Now there are 3 different ways to use reactive variables (and you can mix them in one project) - and joining to new a project made by other people will be a nightmare

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

      Fair enough!

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

    I have to use vue because I just began to work on a new project. But that $ magic looks like something I don’t want to see ever again

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

    So interesting. It is the big change between vue 2 and vue 3!

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

    Thanks for your showcase Erik.
    I prefer using ref and shallowRef only and always add .value because less compiler magic usually leads to better code imho.
    But The vue team definetly does a great job introducing convenience features for simpler use cases.
    For libraries I would stay away from using such features until they are polished enough :)

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

    Thanks. Would love to see another video about reactivity in Vue 2.7 along with a comparison of composition api between 2.7/3. This would be helpful with migrations.

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

    I prefer using refs and calling the value method. It lets me know at a glance that the object I'm working with is reactive.

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

    just going to be honest, I find the options API cleaner, then again I am a Laravel(PHP)+JS developer, not a "pure" JS developer

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

      I think that definitely, the Composition API and the "script setup" are the most "Vue" way to write code. Less code, clearer, cleaner... more Vue.
      I'm just learning Laravel 9, but Vue code I prefer to write with the Composition API.
      Greetings!

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

    All of this just makes me miss Vue 2 😂

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

    Well, in my opinion..
    I'd prefer how vue 2 treat data / value in a simplest way..
    Vue 3 make me learn Vue start from all over again.

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

    Great explanation and examples Eric, you are a natural teacher. While i can see the benefits of using $ and $$, from a readability perspective i find them unintuitive and confusing... more like syntatic salt than syntactic sugar.

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

      Glad it was helpful! I understand it's a little confusing.

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

    This was eventually deprecated so I don't recommend it for new learners, but I appreciate Erik taking the time to go over it.

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

    Vue in general suffers from being insufficiently opinionated. Having ref + reactive + reactivitytransform, adds obscure, difficult-to-reason about syntax.... to what end? Save a few characters? All the $$ stuff makes it clear that this doesn't even abstract anything away from you; you still have to be very careful to not break reactivity. God forbid different developers mix the $ and ref styles within a codebase.
    Pick a best practice. Use only ref always. Be explicit about .value since JS reactivity is always a leaky abstraction, even (especially?) with reactivity transform.

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

    *Important note:* This feature will be removed from Vue. But you will probably be able to use it again via plugin. Personally, I would not necessarily do that and can understand the reasons why this function is removed.

    • @it_is_ni
      @it_is_ni 10 หลายเดือนก่อน

      @programwitherik I think it's a good idea if you update the description of the video, or add a card or something.

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

    Vue is moving toward Svelte way of handling state, because with Svelte a state is simply a variable, we dont need to import 'ref' or 'useState', we simply do let msg = 'message' and it is automatically equipped reactivity feature, I assume all frameworks are mimicing Svelte/Sveltekit 😘

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

      It’s not a bad way to go

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

      Does svelte do any tree-shaking for non-reactive variables? I could see that creating a lot of bloated code when building.

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

      Sometimes I want to make a variable without reactivity, because its not needed. So there should be a way to explicitly make something reactive or not.

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

      @@yourivanmill Yah, simply put 'const' in front of a variable and this makes it non-reactive 😌

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

      @@Vietnamkid1993 nope, compiled codes are clean and simple, of course it does tree-shaking 😌

  • @kylelogue9408
    @kylelogue9408 6 หลายเดือนก่อน +1

    Just an FYI, if you look at the docs about the Reactivity Transform, this experimental feature was removed.

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

    Thanks for the video. It was helpful. Reactivity sure has got complicated, throw in nuxt 3 and it seems to get worse

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

    I am using nuxt and vuetiify now to build stuff. It feels way nicer than this. I built a few apps with react, svelte and angular and now I'm trying to use vue because I've heard it's the simplest of the big 3, but I feel like maybe that was the case before and they are trying to add all the cool modern stuff and completely changing things in major releases. It confuses me when I'm googling problems. All the magic it does behind the scenes makes it harder to grasp. Angular is known as the most complicated, but at this day I would call it simplest of the 3 to grasp because you can figure out everything that is going on. It is very explicit. And svelte feels like the simple amazing one at the moment. What do you think of the differences? Are they complicating our lives with new releases or making them easier?

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

    I'm struggling to understand why he faced this problem in the first place. Why would someone write the Vue component's script block this way and not keep the variables in the "data" block where they already are reactive...?

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

      I guess because it's not just the data block, if you're using the option api, you also have to make blocks for everything else, methods, computed, and so on.
      That's a lot of lines for nothing and sometimes you have to makes variables reactive so they can be accessed from 2 methods. Whereas the composition api allows you to make non-reactive variables and use them everywhere you want.

  • @dev-am1303
    @dev-am1303 ปีที่แล้ว

    I like your lego set :D and thanks for the video.

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

    I think it's a cool way to do things in Vue, but ... Reactvity Transform is still an experimental feature of Vue 3. So it's "risky" to use it in production. It would be nice if the Vue team made it permanent, but we don't know what will really happen.
    Cool video. Thanks for sharing it!
    Blessings from Venezuela, and happy new year 🤗

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

      Yeah, it was supposed to be out in 3.3 , but it seems it has been pushed

  • @saivaraprasadarao6990
    @saivaraprasadarao6990 7 หลายเดือนก่อน

    Great explanation , and Thank you so much for the explanation.
    How can we show modal popup with a div tag(textbox and button) in it, using ref in Vue3 and Bootstrap 5. Could you please share some suggestions on this. It will help me a lot . Thanks in advance

  • @isan-sunshine
    @isan-sunshine ปีที่แล้ว +1

    I’m not a fan of $ syntax, i prever .value since it’s easier to understand even thought i’ll have to write extra code

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

    Awesome, thanks

  •  ปีที่แล้ว +1

    You did not mentioned that reactive will update render just for properties which are updated and if you have object in ref, it will update whole render.

  • @JulioGonzalez-ou4nw
    @JulioGonzalez-ou4nw ปีที่แล้ว

    I like your lego. I think you are right, looks kind of awesome.

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

    Great looking background. 👌

  • @RockTheCage55
    @RockTheCage55 7 หลายเดือนก่อน

    IMO especially for consistency in a group use Ref always......would be a lot less confusing you will just have to remember in scripts to do `.value`. I don't want to ever think about having to do `.ToRef()`

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

    update, removed from core 3.4 but still available as a package

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

    At 5:40 can you do a spread, so msg = {...newNum}, where newNum = {num: 15}? And if so would something like num = 10; msg = {...{num}}?

  • @phillbaska
    @phillbaska 7 หลายเดือนก่อน

    i've been working on (learning) Vue 3 on a customisable dashboard project using Pinia. The ref/reactive was/is a pain point for me, especially when it comes to overwriting entire objects, as a lowly beginner this was very confusing.

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

    Adding ".value" is a cue for myselt that yeaah, "that value" is definitely reactive.

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

    That's cool, I love Composition API...
    Anyways, .value is not a problem for me xD

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

    Less magic is always better. And ts helps to know what is what.

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

    I don't want to type .value and still have the reactivity without the overhead, not possible with vue?

  • @TheDetroja
    @TheDetroja 2 หลายเดือนก่อน

    what version are you using ?

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

    i use reactive every time i can. i feel like the $ is makes your code a little less readable. not sure if i am going to use it.

  • @kirayamato6128
    @kirayamato6128 4 หลายเดือนก่อน

    .value actually good so that we can differentiate the reactive variable or just a variable

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

    I’m gonna be honest here, I see that a lot of people like vue and think it’s really intuitive. And I think many points are fair. But are there really people out there that say: “Yeah, this is better than React”? I’m quite confused by the many different ways to achieve reactivity, but then again, if you destructure it, you might lose that reactivity and need $-signs, or .value or whatever, depends on the implementation. I think vue has a lot of good stuff, but React’s implementation of state seems so much better and with less pitfalls. Also, at 12:47, Erik talked about a “store”. From my understanding this will still create new state for each component. Or is there some magic happening so that’s global, shared state?

  • @BalazsBohonyi
    @BalazsBohonyi ปีที่แล้ว +7

    Hell of a lot more confusing with $ than just using .value

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

    .value is needed in the javascript but you can mutate without .value in the template

  • @1973courage
    @1973courage ปีที่แล้ว +2

    Very complex. I would say, Svelte uses smarter syntax.

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

      I didn't explain it very well on this. I'll make a follow up video.

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

    I do mind the .value - and as soon as your app gets closer to enterprise scale you will too.

  • @user-hv7kc5zy6f
    @user-hv7kc5zy6f 6 หลายเดือนก่อน

    Why can't I use $ref in nuxt3 project?

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

    Ty for video! Pls stop scroll up and down in 14:18 ))

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

    Server is running correctly but I got an type error(ts2552) with $ref on vscode!

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

      That is weird... oh yeah, make sure you turn the feature on in you config.

  • @khaldounal-nuaimi3594
    @khaldounal-nuaimi3594 ปีที่แล้ว +3

    I am honestly not sure what problems the $ addresses. I have worked with a decent number of Vue applications and faced no problems with the .value . Maybe I am just set in my ways, but I prefer the .value to the newly proposed $ method.

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

      It's just a vue-only thing that isn't so intuitive, especially if coming from Vue2. Having to add .value everywhere is just more typing and pretty ugly imo. If I had learned it this way initially, I'd probably be feeling the same way you are. But once you learn it, and accept it, it's fine. Just an initial learning curve and mindshift.

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

      I don't know why he said it was a "problem" per se. It's syntactic sugar for people who like to write less code. I am in the same boat. I prefer writing .value because $ reminds me too much of jQuery.

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

    a mess with $'s

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

      dollar sign frenzy

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

    So reactive is just ref with extra steps?

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

      Other way around. ref is a reactive that exposes a value property.

  • @serhiicho
    @serhiicho 2 หลายเดือนก่อน +1

    This feature has been dropped in version 3.4

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

    Great video.. but $$ .. and toRef.. definitely its a mess

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

    reactive uses ref under the hood I'm pretty sure. I haven't really found a specific case for only using reactive.

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

      Lol, ref using reactive

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

      That’s a good point , I have a soft spot for Reactive though

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

    I still think this $ feature, will make who doesn't understand reactivity feel more confuse.

  • @kstan79
    @kstan79 หลายเดือนก่อน

    Remove .value simply confuse us whether it is reactive variable or not. No doubt code shorter

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

    Early on vue really ate reacts lunch when it came to the quality of their API but all this special character, obfusticated variable types nonsense makes for a terrible interface. React is treating primitive JavaScript objects in the right way and this is getting further away from that.

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

    Didn't like it at all, confusing. Using composables that return refs wrapped in object and have no problem with .value efforts. Worries me a bit as it doesn't seem to be a right direction.

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

    "Stop using .value" is a good title because call to my attention but at the same time it is a clickbait (no offense please) because there is nothing bad using ref(type). In fact, ref and Ref are more powerful because I can define variables in TypeScript like this:
    const myVar: Ref = ref(null);
    Of course, we are obligated to use myVar.value but this is a small price to pay for.

  • @виртуоз_ру
    @виртуоз_ру ปีที่แล้ว

    I will use it or not, but it was interesting. Thanks.

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

    With ref and reactive, Vue has lost one of it's main benefit, small learning curve.
    Also Vue dev tools doesn't work properly.
    Till now I have only used Vue but now I'm thinking of shifting to react or enjoy svelte.
    One thing Vue developers could do is make every variable with $ sign as reactive property, hence completely eliminating the use of ref and reactive from beginners standpoint.
    For eg $first_name, $useSuperhero()
    I kindly request to Vue developers to not make it complex just for the top developers. There are low level developers also.

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

      Sorry to hear that. I probably could have explained this better. Don't give up on it!

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

    I literally never use reactive

  • @Sk8nRock
    @Sk8nRock 11 หลายเดือนก่อน

    Options API: look at all the hoops they have to jump through just to mimic a fraction of our power!!!

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

    Rest in peace Vue.js 😢

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

      It's not dead yet!

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

      @@ProgramWithErik "Vue 2.7, which was shipped in July 2022, is the final minor release of the Vue 2 version range. Vue 2 has now entered maintenance mode: it will no longer ship new features, but will continue to receive critical bug fixes and security updates for 18 months starting from the 2.7 release date. This means Vue 2 will reach End of Life on December 31st, 2023."

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

    Reactivity transform depreciated. Lmao

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

    They will be removing this feature in 3.4

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

    But you dont explain what a $ or a $$ does, or why inside a function. If I dont understand it, i prefer the known way.

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

    Oh god. Keep things simple. :(

  • @ignisAnimus
    @ignisAnimus 11 หลายเดือนก่อน

    The feature has unfortunately been dropped.

    • @ProgramWithErik
      @ProgramWithErik  11 หลายเดือนก่อน

      Which one?

    • @ignisAnimus
      @ignisAnimus 11 หลายเดือนก่อน

      $ref@@ProgramWithErik

  • @sandorturbucz425
    @sandorturbucz425 6 หลายเดือนก่อน

    This one was rejected by Evan so everyone beware, don't use this. A Vue macro is available for it if really needed but probably not a good idea.

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

    This video doesn't explain, why someone should even consider this experimental featue which provides nothing new. Maybe it's just useless. This video at least doesn't give a reason that justifies "Stop Using ...". Quite bad ...

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

      He's just feeding the algorithm. Click-batey titles for engagement.
      Reactivity Transform has been dropped, I hope he updates everyone about this.
      I have a good eye for when a feature is a code smell and I knew this feature wouldn't be welcomed.
      In the other hand, the defineModel, generics, defineSlot, defineOptions, are amazing. And Nuxt with the auto import everything (stores, layouts, pages, components, stores and custom directories, server api paths completion) it's even better!

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

    Composition api looks not so good. I think this is the main reason that vue is loosing popularity

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

    Warning! Reactivity Transformed is being dropped by Vue in the coming months

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

    Such an overenginnering just for nothing, lmao

  • @Ivan-qp8yd
    @Ivan-qp8yd ปีที่แล้ว +5

    $() $$() It is odd isn't it? Look like JQuery if you want to do this you have to use $$ if you want to do that, please use $() ohhh I think it is not pretty good feature the Vue core team must think how to simlify the framework vue instead of making it more complecated. Vue must be predictable and easy to learn and understand. In the framework too many ways how to create reactive variable It is useless and can bother other developers. One developer likes REF other REACTIVE other $REF what is the hell

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

    I loved Vue for its simplicity. They turned really wrong corner. 🥲

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

      Um. I'm going to make a follow up of this video. I think it came off too confusing.