Why You Should Say Goodbye to Mixins and Embrace Composables
<p>Vuejs 3 introduced composables as the new way to define logic that is shared and re-used among components. Composables were mainly introduced to solve the limitation introduced by mixins. If you are familiar with Vuejs 2 then you have heard of mixins which was the defacto standard to share and reuse code logic among components.</p>
<p>While mixins were great they presented some limitations such as the ones below:</p>
<ol>
<li><strong>Property Source</strong>: When using numerous mixins, it can be challenging to determine which instance property originates from which mixin. This ambiguity makes it difficult to follow the component's logic. Utilizing the refs + destructure pattern with composables enhances clarity by explicitly identifying property sources.</li>
<li><strong>Namespace Collisions:</strong> Different mixins from various sources may unintentionally use the same property keys, causing conflicts. Composables offer the flexibility to rename variables when conflicts arise, sidestepping namespace collisions.</li>
<li><strong>Enhanced Modularity: </strong>Mixins can lead to implicit dependencies among multiple mixins, relying on shared property keys. Composables promote explicit communication by allowing values returned from one composable to be passed as arguments to another, fostering more modular and understandable code.</li>
</ol>
<p>While not entirely deprecated in Vue.js 3, mixins are no longer recommended. Instead, composables are the preferred method for crafting reusable code logic.</p>
<h2>Composables</h2>
<p>We are going to write a simple form validation composable and see how we can re-use the logic in our components.</p>
<p><a href="https://levelup.gitconnected.com/why-you-should-say-goodbye-to-mixins-and-embrace-composables-9749d8fac9fc">Read More</a></p>