Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Marina Mosti
Marina Mosti

Posted on

     

So what actually is Vue.set?

This article was originally published athttps://www.telerik.com/blogs/so-what-actually-is-vue-set


Talking aboutVue.set is talking about reactivity, so prepare yourself for some theory on this one. However, as always, it doesn't need to be something hard or boring. Find your avocados and chips, make some guacamole, and let's dip right in.

Data and Reactivity

Whenever you create adata() property function inside a Vue component, and return the object back, Vue does a lot of things behind the scenes to hook everything up inside your component and to make it reactive.

exportdefault{data(){return{red:'hot',chili:'peppers'}}}

The first thing that Vue will do here with our awesome awesome awesome RHCP data, is walk through each of theproperties of thereturn { } object, and create a uniquegetter andsetter for each one. The nitty gritty of how this actually happens is beyond the scope of this article, butVue Mastery has a very nice video explaining this in detail.

The purpose of creating these is so that when you access these properties inside your code, by doingthis.red for example, or when setting them withthis.red = "hotter" you are actually calling thesegetters andsetters that Vue created for you.

Inside this magical land of SETGET, Vue hooks up your computer properties, watchers, props, data, etc. to becomereactive, in super simple terms, a function is called that updates the whole shebang every time the setter is changed.

The pitfall

Awesome! This is what we love about Vue right, its amazing reactivity and under-the-hood power. But there is a dark side here that needs to explored.

Let's change our data a little bit and see what happens when we start trying to work with dynamic data.

data(){return{members:{}}}

Alright, nothing fancy so far, we have amembers property of our data into which we want to add band member information. Now for the sake of example, let's add a method that will pretend to pull some information from a remote http call, that will give us back a JSON object with the band info.

data(){return{members:{}}},methods:{getMembers(){constnewMember={name:'Flea',instrument:'Bass',baeLevel:'A++'};// Some magical method that gives us data got us this sweet info// ...}}

Hm. Ok, so looking at this example let's stop and think. There are many ways to resolve this current dilema, how do we add thisnewMember object into our currentmembers property?

Maybe you're thinking let's turnmembers into an array andpush it, sure, but that's cheating because it breaks my CAREFULLY constructed example that I didn't just make up as I was typing this.

In this scenario, we NEED to havemembers as an object. Ok, simple - you'd say, let's just add a new property to themembers property, it's an object after all. In fact, let's go ahead and make the member's name the name of the property.

getMembers(){constnewMember={name:'Flea',instrument:'Bass',baeLevel:'A++'// Totally important property that we will never use};// Some magical method that gives us data got us this sweet infothis.members[newMember.name]=newMember;}

Lok'tar Ogar!

Except, no, because A. this is not Orgrimmar and B. we now have a problem.

If you were to run this code on your browser and test it right now, you will see that you are actually pushing this new data into themembers data, but this change to the component state will not actually make any of your application re-render.

In a scenario where you are just using this data for some computation, or for some type of internal storage then doing things this way won't impact your application - however, and this is a huge HOWEVER, if you are using this data reactively on your app to display some information on your page, or for conditional rendering withv-if orv-else then things are going to get funky.

Actually using Vue.set

So now that we understand where the problem is actually coming from, we can learn what is the proper solution. Allow me to introduce you toVue.set.

Vue.set is a tool that allows us to add a new property to an already reactive object, and makes sure that this new property is ALSO reactive. This completely takes care of the problem that we were experiencing on the other example, because when our new property onmembers gets set, it will automatically be hooked into Vue's reactivity system, with all the cool getters/setters and Vue-magic behind the scenes.

A small note is however required to understand how this affects arrays. So far we have only really played around with objects, which are super easy to understand. New prop? Add it with Vue.set if you want it to be reactive. Simple.

Following up with our example, let's switch things up to useVue.set instead.

getMembers(){constnewMember={name:'Flea',instrument:'Bass',baeLevel:'A++'};// Some magical method that gives us data got us this sweet info//this.members[newMember.name] = newMember;this.$set(this.members,newMember.name,newMember);}

This bit is newthis.$set(this.members, newMember.name, newMember);

There's two things I want to mention for this piece of code. I've been telling you so far thatVue.set is how we are going to be doing things, but now im usingthis.$set, but fear not - this is only an alias, so it will do and behave in the exact same way. The cool thing, is that you don't have to import Vue inside your components to use it.

The second thing I want to make note of is the syntax of this function. It takes three paramters, theobject orarray which we are going to modify (in this casethis.members).

A second paramter which points to theproperty orkey of the first object/array that we passed (sonewMember.name because we want it to be dynamic).

And finally a third paramter which is the value we want to set into it. (In our case,newMember).

this.members[newMember.name]=newMember;//            V               V              Vthis.$set(this.members,newMember.name,newMember);

(PS. My ASCII skills are not for sale ^)

But what's up with array reactivity?

When we create anarray inside the initial state, Vue sets it up to be reactive for us, however Vue is unable to currently detect when you directly assign a value using an index. For example, if we were doing this:

this.membersArray[3]=myNewValue;

Then Vue won't be able to detect this change, and thus it would not be reactive. Please keep in mind that if you modify arrays with operations likepop,splice,push then those operations WILL trigger reactivity on the arrays, so you can safely use those.

In the off case that you need to set an index value directly, we haveVue.set to help us out. Let's see how that would look on our previous example.

this.$set(this.membersArray,3,myNewValue)

If you want to read more about all the reactivity caveats check out thislink to the official documentation

Vue 3.0

This is all still subject to change at the time of this writing, but the word on the street is that these caveats will no longer be an issue. In other words, in Vue 3.0 you will be safe to forget about these edge cases completely, with the exception of those poor souls that still have to target some old browsers that will not fully support the new reactivity system.

Top comments(19)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
glennmen profile image
Glenn Carremans
Native Android developer/Consultant for Appwise, I work on custom projects for clients.PHP/JS (web) developer in my freetime. Trying to keep learning in an ever changing tech world.
  • Email
  • Location
    Belgium
  • Work
    Android Developer/Consultant at Appwise
  • Joined

Love the WoW reference!

CollapseExpand
 
marinamosti profile image
Marina Mosti
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
  • Joined

For the horde.

CollapseExpand
 
nathanbland profile image
Nathan Bland
Software engineer, I love building things with node and javascript. If I'm not writing code, I'm probably outside doing something in the mountains or playing Rocket league
  • Joined

Great article. Been using Vue for quite a while and didn't know about this...

Only problem is now I want chips and guacamole...

CollapseExpand
 
marinamosti profile image
Marina Mosti
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
  • Joined

You should ALWAYS pair Vue with guacamole. 🤨

CollapseExpand
 
dennisk profile image
Dennis Keirsgieter
  • Location
    Netherlands
  • Work
    Lead Full Stack Developer at Learned
  • Joined

Nice post, i never knew this but then again i never used it before since i never run into this issue i suppose?

(PS: The closing parenthesis for the ASCII ps triggered me!)

CollapseExpand
 
marinamosti profile image
Marina Mosti
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
  • Joined

Lucky you! Vue 3 will make this so easy :)

CollapseExpand
 
dennisk profile image
Dennis Keirsgieter
  • Location
    Netherlands
  • Work
    Lead Full Stack Developer at Learned
  • Joined

Hooray! Are you actually looking forward to Vue 3 all together tho? Whenever i like into the proposals it sometimes also scares me off...

Thread Thread
 
marinamosti profile image
Marina Mosti
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
  • Joined

Im totally looking forward to it. Vue 3 will bring a lot more flexibility to the table, and don't worry, once you get your hands dirty and actually force yourself to use it you will find that its a lot easier than it looks. Hopefully soon ill start making some intro tutorials as well, keep tuned :)

CollapseExpand
 
elenakout profile image
elenakout
  • Location
    Thessaloniki
  • Education
    CS Graduate
  • Work
    Freelance
  • Joined

Always inspiring!
Great article
Thank you!

CollapseExpand
 
marinamosti profile image
Marina Mosti
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
  • Joined

Thank YOU for reading ~

CollapseExpand
 
drmzio profile image
Danny ✪
  • Joined

Totally forgot about this! Thanks!

CollapseExpand
 
marinamosti profile image
Marina Mosti
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
  • Joined

-^ Thanks for reading

CollapseExpand
 
valentinprgnd profile image
Valentin Prugnaud 🦊
🦊 Writing about developer experience, leadership, and mental health🐻 Co-Founder/CTO at Speakbox🤓 Software Engineer🎸 Amateur musicianTechstars Boston Alumni W21
  • Location
    Vancouver, BC
  • Pronouns
    He/him
  • Work
    Software Engineer @ TELUS / Entrepreneur
  • Joined

It's a good refresher, thanks! :)

CollapseExpand
 
marinamosti profile image
Marina Mosti
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
  • Joined

Glad you enjoyed it :)

CollapseExpand
 
imtiyaz profile image
Mohammed Imtiyaz
Product Manager at Curatora.io, Full-Stack Engineer, An Enthusiast and WordPress Geek.
  • Location
    Hyderabad
  • Education
    Master of Technology in Computer Science and Engineering.
  • Work
    Product Manager at Curatora.io
  • Joined

Thank you Marina for good explanation.

CollapseExpand
 
marinamosti profile image
Marina Mosti
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
  • Joined

Glad you enjoyed it!

CollapseExpand
 
orby32 profile image
Or Ben-yossef
  • Location
    Israel
  • Work
    Front end developer
  • Joined

Great & interesting article. Wondering if you are mentoring 1:1

CollapseExpand
 
reng99 profile image
call me R
  • Location
    Canton
  • Joined

Hi! What is "RHCP data" mean?

CollapseExpand
 
marinamosti profile image
Marina Mosti
Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
  • Joined

RHCP means Red Hot Chili Peppers, it's a great band with a god for a bass player :D

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Marina Mosti is a full-stack web developer with over 14 years of experience in the field. She enjoys mentoring other women on JavaScript and her favorite framework, Vue.
  • Joined

More fromMarina Mosti

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp