Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
Vue 3 Composition API state not updating when use function

I have wasted my time for this. I am using Vue 3 options API previously and I am trying change to compostion API. I want to push an object to array but the state doesn't update value, why this happen?

<script>
 
export default {
  setup() {
    const state = reactive([])

    function myFunction() {
        state.push({'item':1})
        console.log(state); // state change
    }

    watchEffect(() => {
        console.log(state) // state doesn't change
    })
        
    return {
      myFunction
    }
  }
}

</script>
<template>
  <button @click="myFunction()">Click Here</button>
</template>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have a syntax error in your code. The function declaration should be as function myFunction() { ... instead of function myFunction {...

Always check the browser console for any error first before asking on internet...

const app = Vue.createApp({
  setup() {
    const state = Vue.reactive([])

    function myFunction() {
      state.push({
        'item': 1
      })
      console.log(state); // state change
    }

    Vue.watchEffect(() => {
      console.log(state) // state doesn't change
    })

    return {
      myFunction
    }
  }
})
app.mount("#app")
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.26/dist/vue.global.min.js"></script>
<div id="app">
  <button @click="myFunction()">Click Here</button>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda