Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this.$forceUpdate equivalent in Vue 3 - Composition API?

In Vue 2, instance method this.$forceUpdate() could be used to update the component manually. How can we force update component in Vue 3 - Composition API (inside setup method) ?

setup(props, context) {
   const doSomething = () => {
     /* how to call $forceUpdate here? */
   }

   return {
       doSomething
   }
}

Thanks, in advance.

like image 562
saibbyweb Avatar asked May 10 '21 11:05

saibbyweb


People also ask

Can I use Vue 3 without composition API?

Vue 3 does not require using the Composition API.

What is Vue forceUpdate?

Better way: You can use forceUpdate Normally, Vue will react to changes in dependencies by updating the view. However, when you call forceUpdate , you can force that update to occur, even if none of the dependencies has actually changed.

What is composition API in Vue 3?

Composition API is a set of APIs that allows us to author Vue components using imported functions instead of declaring options. It is an umbrella term that covers the following APIs: Reactivity API, e.g. ref() and reactive() , that allows us to directly create reactive state, computed state, and watchers.

How do I install composition API in Vue 3?

You must install @vue/composition-api as a plugin via Vue. use() before you can use the Composition API to compose your component. 💡 When you migrate to Vue 3, just replacing @vue/composition-api to vue and your code should just work.


2 Answers

When I need to force an update in vue I usually add a key with a value I can change, which will then force vue to update it. That should work in vue 3 as well, though I admit I haven't ever tried it. Here's an example:

<template>
  <ComponentToUpdate
    :key="updateKey"
  />
</template>

<script>
  export default {
    data() {
      return {
        updateKey: 0,
      };
    },
    methods: {
      forceUpdate() {
        this.updateKey += 1;
      }
    }
  }
</script>

You can read more about it here: https://michaelnthiessen.com/key-changing-technique/

like image 160
Hazza Avatar answered Sep 27 '22 22:09

Hazza


$forceUpdate is still available in Vue3, but you won't have access to it in the setup() function. If you absolutely need to use it, you can use object API component or this fancy trick...

app.component("my-component", {
  template: `...`,
  methods: {
    forceUpdate(){
        this.$forceUpdate()
    }
  },
  setup(props) {
    const instance = Vue.getCurrentInstance();
    Vue.onBeforeMount(()=>{
        // instance.ctx is not populated immediately
        instance.ctx.forceUpdate();
    })
    return {doSomething};
  },
})

If this seems like a ridiculous solution, trust your Judgement. Ideally your application would not rely on forceUpdate. If you are relying on it, it likely means that something is miss-configured, and that should be the first thing to resolve.

like image 25
Daniel Avatar answered Sep 27 '22 23:09

Daniel