Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly reset Vue Composition Api's reactive values

I'm wondering how should I reset a reactive in vuejs setup? (i know if change it to the ref and using view.value will solve this problem, but there should be an answer to this for using reactive)

enter image description here

like image 359
SeyyedKhandon Avatar asked Apr 13 '20 09:04

SeyyedKhandon


People also ask

What is reactive in composition API?

Reactivity is a key pillar for building VueJS applications. Vue 3 Reactivity with Composition API is driven by reactive() and ref() functions. These functions turn the component model data reactive so that Vue is able to track changes. However, both reactive() and ref() have their specific use-cases.

What is the point of reactivity in Vue?

Vue's reactivity system works by deeply converting plain JavaScript objects into reactive proxies. The deep conversion can be unnecessary or sometimes unwanted when integrating with external state management systems (e.g. if an external solution also uses Proxies).

Should you use composition API as a replacement for VUEX?

Conclusion. There's no harm in creating a simple module for global reactive state using Composition API in a small project, or if it's really important to avoid any overhead.

What is Vue 3 reactivity with composition API?

Thankfully, Vue 3 Reactivity with Composition API is equally robust. Vue 3 Reactivity with Composition API is driven by reactive () and ref () functions. These functions turn the component model data reactive so that Vue is able to track changes. However, both reactive () and ref () have their specific use-cases.

What is the difference between ref () and reactive () in Vue?

Let’s start with the basics: you must use ref () to create a reactive primitive value. reactive (), on the other hand, can only be used for creating reactive objects. You can use it as a replacement for the old data option in standard Option API-based Vue components, for example.

What are the disadvantages of using reactive() API?

We can use reactive () API to only declare objects, arrays or other collection types such as Map or Set. Basically, reactive () function does not work for primitives such as string, integer, boolean etc. In my view, this is a big disadvantage that limits the use of reactive () API.


2 Answers

Object.assign didn't work for me. (Maybe because I used a shim for the Composition API in Nuxtjs 2?) For anybody that run into the same problem: I had to use a compact loop.

  setup() {
    const initialState = {
      name: "",
      lastName: "",
      email: ""
    };

    const form = reactive({ ...initialState });

    function resetForm() {
        for (const [key, value] of Object.entries(initialState)) {
          form[key] = value
        }
    }

    function setForm(values = {name: "John", lastName: "Doe", email: "[email protected]"}) {
        // only loop with the supported keys of initial state
        for (const key of Object.keys(initialState)) {
          form[key] = values[key]
        }
    }

    return { form, setForm, resetForm };
  }
like image 169
Markus Madeja Avatar answered Nov 24 '22 06:11

Markus Madeja


You can use Object.assign:

  setup() {
    const initialState = {
      name: "",
      lastName: "",
      email: ""
    };

    const form = reactive({ ...initialState });

    function resetForm() {
      Object.assign(form, initialState);
    }

    function setForm() {
      Object.assign(form, {
        name: "John",
        lastName: "Doe",
        email: "[email protected]"
      });
    }

    return { form, setForm, resetForm };
  }

See it live on codesandbox

credits: taken from here

like image 30
Roland Avatar answered Nov 24 '22 07:11

Roland