Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue3 performance warning using ref

vue is throwing this message:

Vue received a Component which was made a reactive object. This can lead to unnecessary performance overhead, and should be avoided by marking the component with markRaw or using shallowRef instead of ref.

<template>
      <component v-for="(el, idx) in elements" :key="idx" :data="el" :is="el.component" />
</template>



 setup() {
    const { getters } = useStore()
    const elements = ref([])
    onMounted(() => {
      fetchData().then((response) => {
        elements.value = parseData(response)
      })
    })
    return { parseData }
}

is there a better way to do this?

like image 913
claud.io Avatar asked Jan 14 '21 09:01

claud.io


People also ask

What is ref in vue3?

ref is a special attribute, similar to the key attribute discussed in the v-for chapter. It allows us to obtain a direct reference to a specific DOM element or child component instance after it's mounted.

What is ref function in Vue?

Refs are Vue. js instance properties that are used to register or indicate a reference to HTML elements or child elements in the template of your application. If a ref attribute is added to an HTML element in your Vue template, you'll then be able to reference that element or even a child element in your Vue instance.

When to use Ref () in Vue option?

You can use it as a replacement for the old data option in standard Option API-based Vue components, for example. But you can also use ref () for that. In the following example, we use ref () to create a reactive object.

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.

How to avoid performance overhead when using a React component in Vue?

Vue received a Component which was made a reactive object. This can lead to unnecessary performance overhead, and should be avoided by marking the component with markRaw or using shallowRef instead of ref.

Why is my vue3 event not updating?

The reason is referring to the scope variable (id) in the event handlers @set-checked and @rename. In vue3 a scope variable in an event handler cannot be cached, which means that every update will create a new event function, which will cause the component to update.


Video Answer


2 Answers

First, you should return { elements } instead of parseData in your setup i think.


I solved this issue by marking the objects as shallowRef :

import { shallowRef,  ref, computed } from 'vue'
import { EditProfileForm, EditLocationForm, EditPasswordForm} from '@/components/profile/forms'

const profile = shallowRef(EditProfileForm)
const location = shallowRef(EditLocationForm)
const password = shallowRef(EditPasswordForm)

const forms = [profile, location, password] 
<component v-for="(form, i) in forms" :key="i" :is="form" />

So you should shallowRef your components inside your parseData function. I tried markRaw at start, but it made the component non-reactive. Here it works perfectly.

like image 70
Benjamin Fourgeaud Avatar answered Nov 06 '22 17:11

Benjamin Fourgeaud


you could manually shallowCopy the result

<component v-for="(el, idx) in elements" :key="idx" :data="el" :is="{...el.component}" />
like image 31
ali emili Avatar answered Nov 06 '22 17:11

ali emili