How to watch props change with Vue Composition API / Vue 3?
Asked Answered
N

9

176

While Vue Composition API RFC Reference site has many advanced use scenarios with the watch module, there are no examples of how to watch component props.

Neither is it mentioned in Vue Composition API RFC's main page or vuejs/composition-api in Github.

I've created a Codesandbox to elaborate on this issue.

<template>
  <div id="app">
    <img width="25%" src="./assets/logo.png">
    <br>
    <p>Prop watch demo with select input using v-model:</p>
    <PropWatchDemo :selected="testValue"/>
  </div>
</template>

<script>
import { createComponent, onMounted, ref } from "@vue/composition-api";
import PropWatchDemo from "./components/PropWatchDemo.vue";

export default createComponent({
  name: "App",
  components: {
    PropWatchDemo
  },
  setup: (props, context) => {
    const testValue = ref("initial");

    onMounted(() => {
      setTimeout(() => {
        console.log("Changing input prop value after 3s delay");
        testValue.value = "changed";
        // This value change does not trigger watchers?
      }, 3000);
    });

    return {
      testValue
    };
  }
});
</script>
<template>
  <select v-model="selected">
    <option value="null">null value</option>
    <option value>Empty value</option>
  </select>
</template>

<script>
import { createComponent, watch } from "@vue/composition-api";

export default createComponent({
  name: "MyInput",
  props: {
    selected: {
      type: [String, Number],
      required: true
    }
  },
  setup(props) {
    console.log("Setup props:", props);

    watch((first, second) => {
      console.log("Watch function called with args:", first, second);
      // First arg function registerCleanup, second is undefined
    });

    // watch(props, (first, second) => {
    //   console.log("Watch props function called with args:", first, second);
    //   // Logs error:
    //   // Failed watching path: "[object Object]" Watcher only accepts simple
    //   // dot-delimited paths. For full control, use a function instead.
    // })

    watch(props.selected, (first, second) => {
      console.log(
        "Watch props.selected function called with args:",
        first,
        second
      );
      // Both props are undefined so it's just a bare callback func to be run
    });

    return {};
  }
});
</script>

Although my question and code example were initially with JavaScript, I'm using TypeScript. Tony Tom's first answer although working, led to a type error. Which was solved by Michal Levý's answer. So I've tagged this question with typescript afterward.

Here is my polished yet barebones version of the reactive wirings for this custom select component, on top of <b-form-select> from bootstrap-vue (otherwise agnostic-implementation but this underlying component does emit @input and @change events both, based on whether the change was made programmatically or by user interaction).

<template>
  <b-form-select
    v-model="selected"
    :options="{}"
    @input="handleSelection('input', $event)"
    @change="handleSelection('change', $event)"
  />
</template>

<script lang="ts">
import {
  createComponent, SetupContext, Ref, ref, watch, computed,
} from '@vue/composition-api';

interface Props {
  value?: string | number | boolean;
}

export default createComponent({
  name: 'CustomSelect',
  props: {
    value: {
      type: [String, Number, Boolean],
      required: false, // Accepts null and undefined as well
    },
  },
  setup(props: Props, context: SetupContext) {
    // Create a Ref from prop, as two-way binding is allowed only with sync -modifier,
    // with passing prop in parent and explicitly emitting update event on child:
    // Ref: https://v2.vuejs.org/v2/guide/components-custom-events.html#sync-Modifier
    // Ref: https://medium.com/@jithilmt/vue-js-2-two-way-data-binding-in-parent-and-child-components-1cd271c501ba
    const selected: Ref<Props['value']> = ref(props.value);

    const handleSelection = function emitUpdate(type: 'input' | 'change', value: Props['value']) {
      // For sync -modifier where 'value' is the prop name
      context.emit('update:value', value);
      // For @input and/or @change event propagation
      // @input emitted by the select component when value changed <programmatically>
      // @change AND @input both emitted on <user interaction>
      context.emit(type, value);
    };

    // Watch prop value change and assign to value 'selected' Ref
    watch(() => props.value, (newValue: Props['value']) => {
      selected.value = newValue;
    });

    return {
      selected,
      handleSelection,
    };
  },
});
</script>
Nevermore answered 1/12, 2019 at 13:14 Comment(4)
Why can't you just use the watch on the props which you take into the setup function? First make them into `Refs, basiacally make a reactive copy and it should fire on subsequent changes.Sommersommers
This isn't the right question. We shouldn't have to watch props in Vue! The fact that you can't destructure props like we could in Vue 2 seems like a big step backwards. See "toRefs" (and in the future "toRef") to see how you can avoid this anti-pattern of watching a prop just to set another value.Yuille
I've added an alternative answer if you would rather keep the props reactive vs have to write "watch" code all the time.Yuille
might be useful: vue: why do you need to use an arrow function to watch a prop with the composition API?Samarskite
A
239

If you take a look at watch typings here it makes it clear the first argument of watch could be an array, function or Ref<T>

props passed to the setup function are a reactive object (likely by readonly(reactive()), it's properties are getters. So what you are doing is passing the value of the getter as the 1st argument of watch, string "initial" in this case. Because Vue 2 $watch API is used under the hood (and same function exists in Vue 3), you are effectively trying to watch non-existent property with name "initial" on your component instance.

Your callback was only called once. The reason it was called at least once is because the new watch API is behaving like the current $watch with the immediate option (UPDATE 03/03/2021 - this was later changed and in release version of Vue 3, watch is lazy same way as it was in Vue 2)

So by accident you are doing the same thing Tony Tom suggested but with the wrong value. In both cases, it is not valid code when you are using TypeScript.

You can do this instead:

watch(() => props.selected, (first, second) => {
      console.log(
        "Watch props.selected function called with args:",
        first,
        second
      );
    });

Here the 1st function is executed immediately by Vue to collect dependencies (to know what should trigger the callback) and 2nd function is the callback itself.

Other way would be to convert props object using toRefs so it's properties would be of type Ref<T> and you can pass them as the first argument of watch.

However, most of the time watching props is not needed. Simply use props.xxx directly in your template (or setup) and let Vue do the rest.

Anomaly answered 1/12, 2019 at 15:38 Comment(5)
I've renamed the first parameter as 'newValue'. Having it typed with 'any' works but with specific types am getting a type error: Argument of type '() => string | number | boolean | undefined' is not assignable to parameter of type 'WatcherSource<unknown>[]'. Type '() => string | number | boolean | undefined' is missing the following properties from type 'WatcherSource<unknown>[]': pop, push, concat, join, and 25 more.Nevermore
However, using indexed access type my props type interface is not giving an error: Props['selected']Nevermore
Sorry, I'm not a TypeScript expert (yet). What you want is use 2nd definition from typings referenced in my answer (function watch<T>). Maybe share sandbox with TS solution ? btw do you really need old value in the callback ?Kragh
Actually the second parameter was undefined so not sure how I could access the old value...but that was not needed in this case. I've added an edit to my question with a more polished example for all these reactive wirings.Nevermore
First argument can be a getter/effect function, a ref, a reactive object, or an array of these types.Gaddi
S
78

I just wanted to add some more details to the answer above. As Michal mentioned, the props coming is an object and is reactive as a whole. But, each key in the props object is not reactive on its own.

We need to adjust the watch signature for a value in the reactive object compared to a ref value

// watching value of a reactive object (watching a getter)

watch(() => props.selected, (selection, prevSelection) => { 
   /* ... */ 
})

Note: See comment from Michal Levý below before using this potentially erroneous code:

// directly watching a value

const selected = ref(someValue)

watch(selected, (selection, prevSelection) => { 
   /* ... */ 
})

Just some more info even though it's not the mentioned case in the question: If we want to watch on multiple properties, one can pass an array instead of a single reference

// Watching Multiple Sources

watch([ref1, ref2, ...], ([refVal1, refVal2, ...],[prevRef1, prevRef2, ...]) => { 
   /* ... */ 
})

Stryker answered 22/2, 2020 at 19:15 Comment(2)
one more example: watching ref and reactive (including props) together: watch([ref, () => reactiveObj.property, () => prop.val], ([refNew, reactiveNew, propValNew], [refOld, reactiveOld, propValOld]) => { /* Code here */ });Dotson
I have sort of "deja vu" feeling here as I think I already wrote this comment before but the second example is very dangerous and wrong as it creates new selected ref and initializes it with current value of props.selected. Problem is from that point that selected ref is completely disconnected from the props.selected value (if prop value changes, ref will not). This is in most cases NOT what you want.Kragh
Y
31

This does not address the question of how to "watch" properties. But if you want to know how to make props responsive with Vue's Composition API, then read on. In most cases you shouldn't have to write a bunch of code to "watch" things (unless you're creating side effects after changes).

The secret is this: Component props IS reactive. As soon as you access a particular prop, it is NOT reactive. This process of dividing out or accessing a part of an object is referred to as "destructuring". In the new Composition API you need to get used to thinking about this all the time--it's a key part of the decision to use reactive() vs ref().

So what I'm suggesting (code below) is that you take the property you need and make it a ref if you want to preserve reactivity:

export default defineComponent({
  name: 'MyAwesomestComponent',
  props: {
    title: {
      type: String,
      required: true,
    },
    todos: {
      type: Array as PropType<Todo[]>,
      default: () => [],
    },
    ...
  },
  setup(props){ // this is important--pass the root props object in!!!
    ...
    // Now I need a reactive reference to my "todos" array...
    var todoRef = toRefs(props).todos
    ...
    // I can pass todoRef anywhere, with reactivity intact--changes from parents will flow automatically.
    // To access the "raw" value again:
    todoRef.value
    // Soon we'll have "unref" or "toRaw" or some official way to unwrap a ref object
    // But for now you can just access the magical ".value" attribute
  }
}

I sure hope the Vue wizards can figure out how to make this easier... but as far as I know this is the type of code we'll have to write with the Composition API.

Here is a link to the official documentation, where they caution you directly against destructuring props.

Yuille answered 2/5, 2020 at 16:0 Comment(4)
This is what is needed to be able to re-render the template right? The usage of watch doesn't seem to make sense to me, since I don't have any side effects to do.Impersonality
Great. I went crazy, I missed in the docs 'toRefs'... so, const data = ref(props.data) is not reactive!Zashin
The best solution for meErund
But todoRef would be read-only. As soon as you start mutating it, you're in trouble. A better solution IMO is computed get and set.Flophouse
I
3

In my case I solved it using key

<MessageEdit :key="message" :message="message" />

Maybe on your case would look something like this

<PropWatchDemo :key="testValue" :selected="testValue"/>

But I don't have any idea of its pros and cons versus watch

Interradial answered 13/8, 2022 at 4:46 Comment(1)
a changing key make vue re-render a component, so yes, the value of the prop is reloaded but it not the best solution here as if you don't want the component to be re-rendered it should be able to watch the props change directly to handle them inside. Best usage of key is when using transition or transition-groupAudet
K
1

Change your watch method like below.

 watch("selected", (first, second) => {
      console.log(
        "Watch props.selected function called with args:",
        first,second
      );
      // Both props are undefined so its just a bare callback func to be run
    });
Killie answered 1/12, 2019 at 14:19 Comment(6)
Nice catch! An additional error however, as am using TypeScript - ideas on this? My props argument on setup function does have a custom interface type: Argument of type '"selected"' is not assignable to parameter of type 'WatcherSource<unknown>[]'.Nevermore
This doesn't make sense. Watch source can't be a String.Mons
@Mons Well in Vue 2 the watch argument could be a string (at least in Options API) - in that case the watcher was on the Vue instance (component) property with the same name as the string passed. All the confusion comes from the fact that 1st versions of composition API was build as a plugin on top of Vue 2 options APIKragh
@MichalLevý I did know that. But isn't the question regarding Vue 3?Mons
@Mons Well yes it is now, but when I (and Tony) originally answered it was about Vue2 + Composition API plugin (just watch the history of edits). You can still see a remnants of that in my answer...need to update it I guessKragh
@MichalLevý Ohhh I see. :)Mons
J
0

please note that props cannot be updated directly inside the child component, so no updates, no watch triggers!

if you want to have the updated values, there are several approaches:

  1. use getter setter computed for props that you want to update and then emit them to parent component.
  2. instead of props, use provide/inject ( I know this is when you want to share data in a big component tree! but it comes handy when you have a big form data that needs to be reactive! )
Jamilla answered 13/6, 2023 at 11:37 Comment(0)
U
0

Try this one - works for me:

const currentList = computed(() => props.items)
watch(currentList, (newValue, oldValue) => {
  console.log(newValue,oldValue)
})
Uro answered 13/7, 2023 at 13:46 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Minuteman
B
0

watch stringnumber and boolea props, use arrow function:

watch(() => props.primitiveType,(val,oldVal)=>{})      

watch objand array, use arrow function and deep:

watch(() => props.complexType,(val,oldVal)=>{},{deep:true})

or

watch(props.complexType,(val,oldVal)=>{}) // is ok as well

more info in vue docs

Bivouac answered 26/1 at 6:46 Comment(0)
Y
-3

None of the options above worked for me but I think I found a simple way that seems to works very well to keep vue2 coding style in composition api

Simply create a ref alias to the prop like:

myPropAlias = ref(props.myProp)

and you do everything from the alias

works like a charm for me and minimal

Yarndyed answered 11/9, 2021 at 7:54 Comment(1)
To create an alias you need to use toRefs or toRef. What you creating is not an alias. It is new ref initialized with the current value of prop. When parent updates the prop to a new value, your "alias" will not change. This is in most cases not what you wantKragh

© 2022 - 2024 — McMap. All rights reserved.