Watchers
  • Watchers

    perform side effects in reaction to state changes with Vue watchers.

    10 Questions
  • 1

    Watchers

    What is a watcher in the Vue 3 Composition API?

  • 2

    Syntax

    Which of the following options correctly describes the syntax of a watcher in Vue 3 Composition API?

  • 3

    Syntax

    In a watcher’s callback function, `valueX` argument is the value, while the second argument, `valueY`, is the value of the property being watched.
    watch(x, (valueX, valueY) => {
      console.log(`x is ${newX}`)
    })
  • 4

    Multiple values

    Which of the following options is used to watch multiple reactive data properties in a single watcher?

  • watch vs watchEffect

    watch only tracks the explicitly watched source. Callback from the second argument is only triggered when the source property has changed.

    watchEffect combines dependency tracking and side effect into a single step. watchEffect() tracks the callback's reactive dependencies automatically. So, unlike watch(), no need to define explicit source property as the first argument.

    watchEffect(async () => {
      const response = await fetch(
        `${BaseURL}/todos/${todoId.value}`
      )
      data.value = await response.json()
    })
  • 6

    Options

    Which of the followings are valid options for watch function?

  • 7

    Options

    What is the purpose of the immediate:true option in a watcher?

  • 8

    Options

    Which of the following option can be used to watch nested arrays or objects being watched?

  • 9

    Options

    Which of the following statements are true for the deep:true option of the watch function.

  • 10

    Options

    Which of the following option is applied in watch function to access the DOM inside a watcher callback?

  • Synchronous Watcher

    Most watchers are created synchronously. These synchronous watch functions are stopped automatically when the component where they are declared is unmounted.

    However, when the second argument is created with async callback, this watch function must be stopped manually to avoid memory leaks.`

    Here’s how you can to stop the watcher manually.

    const unwatch = watchEffect(() => {})
    // ...later, when no longer needed
    unwatch()
  • 12

    WatchEffect

    How do you pass flush option in watchEffect function?