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

125
Visualizações
WatchEffect is triggered twice when watching a reactive object

If I run this code:

const state = reactive({
    title: '',
})

watchEffect(() => {
    console.log(state.title)
})

watchEffect is triggered and the console is outputting an empty string:

""

If I want to assign a new value to state.title, watchEffect is triggered twice. Is this behaviour expected or am I doing something wrong?

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

0

According to the documentation, watchEffect

Runs a function immediately [emphasis added] while reactively tracking its dependencies and re-runs it whenever the dependencies are changed.

So it is expected that it should run twice in this situation: once when it is first defined, and then again when the value changes.

about 4 years ago · Juan Pablo Isaza Relatório

0

As @MykWillis pointed out and is clearly stated in the docs, watchEffect runs immediately and on subsequent changes, exactly as watch with { immediate: true } would.

If you do not want the effect to run initially, don't use watchEffect, use watch instead:

const { createApp, watchEffect, reactive, toRefs, watch } = Vue;

createApp({
  setup() {
    const state = reactive({
      title: 'test',
    });

    watchEffect(() => {
      console.log('watchEffect', state.title)
    });

    watch(() => state.title, () => {
      console.log('watch', state.title)
    });

    watch(() => state.title, () => {
      console.log('watch.immediate', state.title)
    }, { immediate: true })
    
    return { ...toRefs(state) }
  }
}).mount('#app')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="app">
  <input v-model="title">
</div>

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