Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can we use a named function in the Vue3 composition API watcher?

I create a watcher within the setup, this works:

    watch(() => variableToWatch, (newVal,oldVal) => {
        console.log(newVal,oldVal)
    })

But this does not (no errors, just doesn’t seem to do anything):

    const fnTest = (newVal,oldVal) => {
        console.log(newVal,oldVal)
    })

    watch(() => variableToWatch, (newVal,oldVal) => fnTest)

Can we not use a function in the watcher callback?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To be fair, I’m still on Vue3, and use the https://github.com/vuejs/composition-api, but that shoulnd’t matter in this case.

>Solution :

Your const "is" the whole function.

So this should work:

    const fnTest = (newVal,oldVal) => {
        console.log(newVal,oldVal)
    })

    watch(() => variableToWatch, fnTest)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading