<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daniel Kasperczyk</title>
    <description>The latest articles on DEV Community by Daniel Kasperczyk (@dankasperczyk).</description>
    <link>https://dev.to/dankasperczyk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2528453%2F23985592-15fe-449d-8bd8-2ee8939694dd.jpg</url>
      <title>DEV Community: Daniel Kasperczyk</title>
      <link>https://dev.to/dankasperczyk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dankasperczyk"/>
    <language>en</language>
    <item>
      <title>When to use composables vs provide/inject in Vue 3</title>
      <dc:creator>Daniel Kasperczyk</dc:creator>
      <pubDate>Mon, 07 Apr 2025 06:47:29 +0000</pubDate>
      <link>https://dev.to/dankasperczyk/when-to-use-composables-vs-provideinject-in-vue-3-4mcm</link>
      <guid>https://dev.to/dankasperczyk/when-to-use-composables-vs-provideinject-in-vue-3-4mcm</guid>
      <description>&lt;p&gt;Vue 3 introduced &lt;strong&gt;composables&lt;/strong&gt; and &lt;strong&gt;provide/inject&lt;/strong&gt;, two powerful patterns for managing state and logic. Both have their place, but choosing the right one at the right time can make your code more &lt;strong&gt;scalable, readable, and maintainable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this article, I'll break down when to use &lt;strong&gt;composables&lt;/strong&gt; and when to use &lt;strong&gt;provide/inject.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Composables: the power of reusable logic
&lt;/h1&gt;

&lt;p&gt;A &lt;strong&gt;composable&lt;/strong&gt; is a function that encapsulates reusable logic using Vue's Composition API. It helps &lt;strong&gt;keep components clean&lt;/strong&gt; by extracting logic into separate functions that can be used across multiple components.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use composables
&lt;/h2&gt;

&lt;p&gt;✅ You need to reuse logic in multiple components.&lt;br&gt;
✅ You're handling &lt;strong&gt;side effects&lt;/strong&gt; (API calls, event listeners, etc.).&lt;br&gt;
✅ You want to keep your components &lt;strong&gt;small and focused&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Example: A &lt;code&gt;useFetch&lt;/code&gt; composable
&lt;/h2&gt;

&lt;p&gt;Let’s create a composable to &lt;strong&gt;fetch data from an API&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// composables/fetch.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useFetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//components/MyComponent.vue&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/template&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="nx"&gt;setup&lt;/span&gt; &lt;span class="nx"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useFetch&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@/composables/useFetch&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useFetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another good examples can be found in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;VueUse GitHub repo: &lt;a href="https://github.com/vueuse/vueuse/tree/main/packages/core" rel="noopener noreferrer"&gt;Link&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Vuetify GitHub repo: &lt;a href="https://github.com/vuetifyjs/vuetify/tree/master/packages/vuetify/src/composables" rel="noopener noreferrer"&gt;Link&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;TanStack VueQuery GitHub repo: &lt;a href="https://github.com/TanStack/query/tree/main/packages/vue-query/src" rel="noopener noreferrer"&gt;Link&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Provide/inject: simplifying state sharing
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;provide/inject&lt;/code&gt; is Vue's dependency injection system. It allows you to pass state from a parent component to deeply nested components, avoiding prop drilling (passing props down multiple levels).&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use provide/inject
&lt;/h2&gt;

&lt;p&gt;✅ You need to &lt;strong&gt;share state&lt;/strong&gt; across deeply nested components.&lt;br&gt;
✅ You want to &lt;strong&gt;avoid prop drilling&lt;/strong&gt;.&lt;br&gt;
✅ You need to provide global configurations (e.g., themes, authentication state).&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: Pass theme to deeply nested components
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Parent.vue (Providing the theme)&lt;/span&gt;
&lt;span class="nf"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;theme&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// Child.vue (Injecting the theme)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;theme&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Default to 'light' if no provider&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another good examples can be found in (search for &lt;strong&gt;provide&lt;/strong&gt; key):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vuetify GitHub repo: &lt;a href="https://github.com/vuetifyjs/vuetify/blob/master/packages/vuetify/src/framework.ts" rel="noopener noreferrer"&gt;Link&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Vue i18n GitHub repo: &lt;a href="https://github.com/intlify/vue-i18n/blob/master/packages/vue-i18n-core/src/i18n.ts" rel="noopener noreferrer"&gt;Link&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Pinia GitHub repo: &lt;a href="https://github.com/vuejs/pinia/blob/v3/packages/pinia/src/createPinia.ts" rel="noopener noreferrer"&gt;Link&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Composables vs. provide/inject: how to choose?
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Use composables when
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;You need to reuse logic across multiple components&lt;/strong&gt; (e.g., fetching data, event listeners, form validation).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You are handling side effects&lt;/strong&gt; like API calls, local storage interactions, or WebSocket connections.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You want to keep components lean&lt;/strong&gt; by extracting complex logic into separate files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You need to keep the state local&lt;/strong&gt; to a specific component tree, &lt;strong&gt;not globally shared&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You want better testability&lt;/strong&gt; – composables make it easier to isolate logic for unit testing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetching data (&lt;code&gt;useFetch&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Detecting screen size (&lt;code&gt;useWindowSize&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Debouncing user input (&lt;code&gt;useDebounce&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Use provide/inject when
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;You need to share state between deeply nested components&lt;/strong&gt; without prop drilling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You have a single source of truth&lt;/strong&gt; (e.g., a global theme or authentication).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You need to pass dependencies down the component tree&lt;/strong&gt;, such as configurations or event buses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You don't need the state to be reactive globally&lt;/strong&gt;, just within a specific component tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You want to improve performance&lt;/strong&gt; by avoiding excessive prop passing and reactivity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Providing a theme or locale (&lt;code&gt;provide('theme', ref('dark'))&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Sharing form state between parent and child components&lt;/li&gt;
&lt;li&gt;Dependency injection for plugins or services&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use composables&lt;/strong&gt;  when you need &lt;strong&gt;reusable logic&lt;/strong&gt; across multiple components, such as API calls, event listeners, or computed values. They help keep components &lt;strong&gt;clean, modular, and testable&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use provide/inject&lt;/strong&gt;  when you need to &lt;strong&gt;share state&lt;/strong&gt; between deeply nested components, avoiding &lt;strong&gt;prop drilling&lt;/strong&gt;. It's great for passing global configs or themes within a specific component tree.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to choose?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Reusable logic → Composables&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Deeply shared state → Provide/Inject&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Getting Started with Vue 3 Plugins: What You Need to Know</title>
      <dc:creator>Daniel Kasperczyk</dc:creator>
      <pubDate>Mon, 31 Mar 2025 05:40:02 +0000</pubDate>
      <link>https://dev.to/dankasperczyk/getting-started-with-vue-3-plugins-what-you-need-to-know-167d</link>
      <guid>https://dev.to/dankasperczyk/getting-started-with-vue-3-plugins-what-you-need-to-know-167d</guid>
      <description>&lt;p&gt;Have you ever wondered what Vue plugins are and how they can enhance your project or improve your skills as a Vue developer? If so, you're in the right place!&lt;/p&gt;

&lt;p&gt;In this article, I’ll break it down so you will have clear view of how plugins can impact your project.&lt;/p&gt;




&lt;h2&gt;
  
  
  When does vue execute plugins?
&lt;/h2&gt;

&lt;p&gt;Before discussing performance and best practices, let's first understand how Vue 3 executes plugins:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Vue initializes the app with &lt;code&gt;createApp(App)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Plugins are installed via &lt;code&gt;app.use(plugin)&lt;/code&gt;. 🚀 &lt;em&gt;(This happens before rendering!)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;App.vue&lt;/code&gt; is mounted, executing its &lt;code&gt;setup()&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;Child components start rendering.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This means that if you use a &lt;code&gt;provide()&lt;/code&gt; in App.vue to share functionalities or information for example about auth, it only becomes available &lt;strong&gt;after&lt;/strong&gt; App.vue is mounted, whereas a plugin runs before rendering begins.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does this matter?
&lt;/h3&gt;

&lt;p&gt;If your app depends on globally available data (such as authentication state), a plugin ensures that data is available &lt;strong&gt;before rendering begins&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why plugins are beneficial?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Separation of Concerns = Better Optimization&lt;/strong&gt;&lt;br&gt;
Keeping global logic (like auth, API clients, or an event bus) inside a plugin removes it from the UI rendering cycle, reducing unnecessary reactivity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ensures Global State is Ready Before Rendering&lt;/strong&gt;&lt;br&gt;
For example, a theme management plugin ensures UI settings are loaded before the app starts rendering.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance Optimization&lt;/strong&gt;&lt;br&gt;
Plugins reduce unnecessary reactivity overhead, unlike provide/inject inside App.vue, which may trigger unwanted re-renders.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easier Scalability for Large Apps&lt;/strong&gt;&lt;br&gt;
As your app grows, keeping global logic inside plugins makes the code modular and easier to maintain. For example, a feature management plugin can dynamically enable or disable features.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Creating a custom Plugin
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// plugins/auth.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;reactive&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;install&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Define reactive authentication state&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;authState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;reactive&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// Mock login function&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;login&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;authState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mocked-jwt-token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;authState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Could use sessionStorage or cookies&lt;/span&gt;
      &lt;span class="nx"&gt;authState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="c1"&gt;// Provide computed properties for better performance&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;authState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;authState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nx"&gt;login&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="c1"&gt;// Provide authentication globally&lt;/span&gt;
    &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;auth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;globalProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$auth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Optional&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When to create custom plugins?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You need global dependencies such as authentication, API clients, event bus, or theme management.&lt;/li&gt;
&lt;li&gt;You need access to certain data before rendering begins.&lt;/li&gt;
&lt;li&gt;You are using Server-Side Rendering (SSR).&lt;/li&gt;
&lt;li&gt;You want better scalability and reusability.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Vue plugins are a powerful way to enhance your application's architecture by keeping global logic modular, improving performance, and ensuring critical dependencies are available before rendering. Whether you're managing authentication, API clients, or theme settings, using plugins can make your codebase more scalable and maintainable.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Create Responsive and Adaptive UI's in Vue with Dynamic Components</title>
      <dc:creator>Daniel Kasperczyk</dc:creator>
      <pubDate>Thu, 05 Dec 2024 13:37:46 +0000</pubDate>
      <link>https://dev.to/monterailhub/create-responsive-and-adaptive-uis-in-vue-with-dynamic-components-h8c</link>
      <guid>https://dev.to/monterailhub/create-responsive-and-adaptive-uis-in-vue-with-dynamic-components-h8c</guid>
      <description>&lt;p&gt;Dynamic components are a powerful feature in Vue.js that help create responsive and adaptive user interfaces tailored to your applications' specific needs and contexts. At our Vue.js agency, we rely on dynamic components to deliver scalable, enterprise-grade, flexible, and maintainable applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Dynamic Components?
&lt;/h2&gt;

&lt;p&gt;Dynamic components eliminate the need for multiple v-if conditions, which can clutter your templates and increase maintenance overhead. With dynamic components, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce repetitive props or actions.&lt;/li&gt;
&lt;li&gt;Enhance readability for more modular, maintainable code.&lt;/li&gt;
&lt;li&gt;Streamline the development process, especially for applications requiring flexible UI structures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this short tip article, you will learn how to use it in your Vue app to make your User Interface more responsive and adaptive.&lt;/p&gt;

&lt;h2&gt;
  
  
  🤔 How to use Dynamic Components in Vue?
&lt;/h2&gt;

&lt;p&gt;To render a dynamic component in Vue, use the  tag with the is attribute. The is attribute accepts a component name or data property, such as is="currentComponent", to determine which component to display dynamically.&lt;/p&gt;

&lt;p&gt;Here’s a basic example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;component :is="currentComponent" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As in other Vue components, we can pass props, react to events, or even create two-way data binding with v-model, as in the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;component :is="currentComponent" :value="value" @input="onInput"/&amp;gt;
// or
&amp;lt;component :is="currentComponent" v-model="value" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Vue removes inactive components from the DOM by default when switching between dynamic components, causing their state to reset. To preserve state across component switches, wrap your &lt;code&gt;&amp;lt;component&amp;gt;&lt;/code&gt; tag with Vue's &lt;code&gt;&amp;lt;keep-alive&amp;gt;&lt;/code&gt; element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;keep-alive&amp;gt;
  &amp;lt;component :is="currentComponent" /&amp;gt;
&amp;lt;/keep-alive&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It caches inactive components, ensuring data like form inputs or sessions persists across views, improving the user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Summary
&lt;/h2&gt;

&lt;p&gt;Dynamic components simplify UI logic, boost efficiency, and enhance maintainability, making them essential for modular code and user engagement. Businesses value Vue.js experts proficient in this feature for professional, scalable solutions.&lt;/p&gt;

&lt;h2&gt;
  
  
  🙋 Want to Dive Deeper?
&lt;/h2&gt;

&lt;p&gt;Explore this topic in greater detail in our blog post, where we’ve covered the &lt;a href="https://www.monterail.com/blog/understanding-dynamic-components-in-vue-3" rel="noopener noreferrer"&gt;Vue 3 Dynamic Components&lt;/a&gt; in building flexible, responsive, and maintainable user interfaces. You’ll learn the fundamentals of dynamic components, including practical implementation and tips. A perfect read for developers looking to enhance their Vue applications with modular and adaptive UI solutions.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>ui</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
