Reactivity using this.$set() with an object not working, but works with an array?

This page summarizes the projects mentioned and recommended in the original post on /r/vuejs

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • Vue.js

    This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core

  • Vue makes result your data() function reactive. So this.reactiveObj (which is set to null) property will be reactive. Then, in mounted(), you assign that property to something else (empty object {}). That signals vue that it needs to rerender that template block {{ reactiveObj }}. But it doesn't rerender immediately. It does so sometime after mounted() has completed. By that time reactiveObj already has some non-reactive properties in it so it happily rerenders it with whatever non-reactive content it has. After that in your button click handler you modify values of those non-reactive properties. And vue doesn't pick up that. Whether you do this with this.reactiveObj[id] = true or this.$set(this.reactiveObj, id, true) does not matter because if you look at Vue.set (or this.$set) source code here https://github.com/vuejs/vue/blob/main/src/core/observer/index.ts (find export function set(...) you'll see

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts