{"id":634,"date":"2024-07-31T14:17:24","date_gmt":"2024-07-31T14:17:24","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=634"},"modified":"2026-02-05T12:06:49","modified_gmt":"2026-02-05T12:06:49","slug":"how-to-watch-props-change-with-vue-composition-api-in-vue-3","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/how-to-watch-props-change-with-vue-composition-api-in-vue-3\/","title":{"rendered":"How to Watch Props Change with Vue Composition API in Vue 3"},"content":{"rendered":"\n<p>The Composition API in Vue 3 has completely changed how developers organize and arrange code in Vue applications, providing a more flexible and structured method. Monitoring changes in props, which are vital for transferring data between components, is crucial in creating Vue components. Using the Vue 3 watch function in particular, the Composition API gives Vue 3 a simple way to watch props and respond dynamically to changes. We&#8217;ll go over how to use Vue 3&#8217;s Composition API to observe props in detail in this post, which will help you become more knowledgeable about Vue programming.<\/p>\n\n\n\n<p><strong>Prerequisites<\/strong><\/p>\n\n\n\n<p>Before we dive in, make sure you have the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Basic understanding of Vue 3 and the Composition API.<\/li>\n\n\n\n<li>Node.js and npm installed on your machine.<\/li>\n\n\n\n<li>A Vue 3 project set up. If not, you can create one using Vue CLI:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install -g @vue\/cli\nvue create my-vue-app\ncd my-vue-app\nnpm run serve<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step-by-Step Guide<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Set Up Your Component<\/h3>\n\n\n\n<p>Let&#8217;s start by setting up a simple Vue component that receives a prop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template>\n  &lt;div>\n    &lt;p>Current count: {{ count }}&lt;\/p>\n  &lt;\/div>\n&lt;\/template>\n\n&lt;script>\nimport { defineComponent } from 'vue';\n\nexport default defineComponent({\n  name: 'Counter',\n  props: {\n    count: {\n      type: Number,\n      required: true\n    }\n  }\n});\n&lt;\/script><\/code><\/pre>\n\n\n\n<p>This component accepts a count prop and displays its value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Watch Prop Changes with a Watch<\/h3>\n\n\n\n<p>Next, we&#8217;ll use the watch function from the Composition API to react to changes in the count prop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template>\n  &lt;div>\n    &lt;p>Current count: {{ count }}&lt;\/p>\n  &lt;\/div>\n&lt;\/template>\n\n&lt;script>\nimport { defineComponent, watch } from 'vue';\n\nexport default defineComponent({\n  name: 'Counter',\n  props: {\n    count: {\n      type: Number,\n      required: true\n    }\n  },\n  setup(props) {\n    watch(() => props.count, (newValue, oldValue) => {\n      console.log(`Count changed from ${oldValue} to ${newValue}`);\n    });\n\n    return {};\n  }\n});\n&lt;\/script><\/code><\/pre>\n\n\n\n<p>Here, we use the watch function to monitor changes to the count prop. The first argument is a function that returns the prop to watch, and the second argument is a callback that receives the new and old values of the prop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Using toRefs for Reactive Props<\/h3>\n\n\n\n<p>For better reactivity, we can use the toRefs function. This allows us to destructure props and maintain reactivity.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template>\n  &lt;div>\n    &lt;p>Current count: {{ count }}&lt;\/p>\n  &lt;\/div>\n&lt;\/template>\n\n&lt;script>\nimport { defineComponent, watch, toRefs } from 'vue';\n\nexport default defineComponent({\n  name: 'Counter',\n  props: {\n    count: {\n      type: Number,\n      required: true\n    }\n  },\n  setup(props) {\n    const { count } = toRefs(props);\n\n    watch(count, (newValue, oldValue) => {\n      console.log(`Count changed from ${oldValue} to ${newValue}`);\n    });\n    return { count };\n  }\n});\n&lt;\/script><\/code><\/pre>\n\n\n\n<p>With toRefs, we convert the props object into individual refs, allowing us to watch count directly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Advanced Usage: Watching Multiple Props<\/h3>\n\n\n\n<p>If you need to watch multiple props, you can do so by passing an array of functions to the watch function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template>\n  &lt;div>\n    &lt;p>Current count: {{ count }}&lt;\/p>\n    &lt;p>Current status: {{ status }}&lt;\/p>\n  &lt;\/div>\n&lt;\/template>\n\n&lt;script>\nimport { defineComponent, watch, toRefs } from 'vue';\nexport default defineComponent({\n  name: 'Counter',\n  props: {\n    count: {\n      type: Number,\n      required: true\n    },\n    status: {\n      type: String,\n      required: true\n    }\n  },\n  setup(props) {\n    const { count, status } = toRefs(props);\n    watch(&#91;count, status], (&#91;newCount, newStatus], &#91;oldCount, oldStatus]) => {\n      console.log(`Count changed from ${oldCount} to ${newCount}`);\n      console.log(`Status changed from ${oldStatus} to ${newStatus}`);\n    });\n\n    return { count, status };\n  }\n});\n&lt;\/script>\n\n<\/code><\/pre>\n\n\n\n<p>This example shows how to watch multiple props using an array.<\/p>\n\n\n\n<p>Watching props in Vue 3 using the Composition API is straightforward and powerful. By leveraging watch and <strong>toRefs<\/strong>, you can easily monitor and react to prop changes in your components. This approach enhances code reusability and organization, making your Vue applications more maintainable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The Composition API in Vue 3 provides strong capabilities like Vue watch and toRefs that make it easier to keep track of prop changes inside of components. Through the utilization of these functionalities, developers may guarantee that their Vue applications react instantly to modifications in data, consequently improving both application performance and user experience. <\/p>\n\n\n\n<p>It is imperative for companies wishing to use Vue.js for web projects to <a href=\"https:\/\/www.cmarix.com\/hire-vuejs-developers.html\">hire Vue.js developers<\/a> who are knowledgeable about the Composition API. Their knowledge guarantees scalability, maintainability, and an effective code structure, resulting in reliable solutions that accomplish corporate goals.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Composition API in Vue 3 has completely changed how developers organize and arrange code in Vue applications, providing a more flexible and structured method. Monitoring changes in props, which are vital for transferring data between components, is crucial in creating Vue components. Using the Vue 3 watch function in particular, the Composition API gives [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":969,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3,152],"tags":[],"class_list":["post-634","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web","category-vue-js"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=634"}],"version-history":[{"count":24,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/634\/revisions"}],"predecessor-version":[{"id":658,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/634\/revisions\/658"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/969"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}