Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
onUpdated is not called with vuejs 3 with render function

I am facing a problem using the onUpdated lifecycle hook in Vuejs3 Composition API. It is not called when a reactive value is updated. I have reproduced the issue with a very simple app. It has one child component:

<script setup lang="ts">
import { h, ref, onUpdated } from 'vue'

const open = ref(false)

const toggle = () => {
    open.value = !open.value
}

defineExpose({ toggle })

onUpdated(() => {
    console.log("Updated")
})

const render = () =>
    open.value ? h(
        'DIV',
        "Child Component"
    ) :
        null
</script>

<template>
    <render />
</template>

Then this component is used by the app:

<script setup lang="ts">
import { ref } from 'vue'
import Child from './components/Child.vue'
const menu = ref(null)
</script>

<template>
  <main>
    <button @click="menu.toggle()">Click Me</button>
    <Child ref="menu" />
  </main>
</template>

<style>
</style>

But when the button is clicked in the app, although the "Child Component" text is shown, proving that the render function is called, the onUpdated callback is not executed.

This must have something to do with the way the render function is called, or the conditional rendering because if I use v-if in a template instead, it works fine. But in my case, I do need an explicit render function.

Can anyone help?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is probably a bug in <script setup>, as that same code works in setup().

A workaround is to switch to setup() if you need to use onUpdated():

<script lang="ts">
import { h, ref, onUpdated } from 'vue'

export default {
  setup(props, { expose }) {
    const open = ref(false)

    const toggle = () => {
        open.value = !open.value
    }

    expose({ toggle })

    onUpdated(() => {
        console.log("Updated")
    })

    const render = () => open.value ? h('DIV', "Child Component") : null

    return render
  }
}
</script>

demo

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda