As applications grow, organizing logic becomes just as important as building features. Vue 3 introduced one of its most powerful improvementsโthe ๐๐ผ๐บ๐ฝ๐ผ๐๐ถ๐๐ถ๐ผ๐ป ๐๐ฃ๐, and with it, the concept of ๐ฐ๐ผ๐บ๐ฝ๐ผ๐๐ฎ๐ฏ๐น๐ฒ๐. Composables let you extract logic out of components and reuse it anywhere in your app. The result? Cleaner components, reusable logic, and a far more scalable architecture.
Letโs take a deep dive into how composables work and why theyโve become essential in modern Vue development.
๐ช๐ต๐ฎ๐ ๐๐ฟ๐ฒ ๐๐ผ๐บ๐ฝ๐ผ๐๐ฎ๐ฏ๐น๐ฒ๐?
A ๐ฐ๐ผ๐บ๐ฝ๐ผ๐๐ฎ๐ฏ๐น๐ฒ๐ is simply a JavaScript function that uses Vueโs reactive features (ref, reactive, computed, watch, etc.) and returns logic you can reuse in multiple components.
They typically follow the naming convention:
๐๐๐ฒ๐ฆ๐ผ๐บ๐ฒ๐๐ต๐ถ๐ป๐ด() โ e.g., useAuth(), useFetch(), useCounter().
๐ช๐ต๐ ๐จ๐๐ฒ ๐๐ผ๐บ๐ฝ๐ผ๐๐ฎ๐ฏ๐น๐ฒ๐?
โ ๐๐น๐ฒ๐ฎ๐ป๐ฒ๐ฟ ๐๐ผ๐บ๐ฝ๐ผ๐ป๐ฒ๐ป๐๐
Business logic moves out of the component, leaving your script section simple and focused.
โ ๐๐ถ๐ด๐ต๐น๐ ๐ฅ๐ฒ๐๐๐ฎ๐ฏ๐น๐ฒ ๐๐ผ๐ด๐ถ๐ฐ
Anything that multiple components shareโform validators, API calls, stateโcan be centralized.
โ ๐๐ฒ๐๐๐ฒ๐ฟ ๐ข๐ฟ๐ด๐ฎ๐ป๐ถ๐๐ฎ๐๐ถ๐ผ๐ป
You can structure your project by domain (auth, user, cart, UI behavior) rather than cramming everything in components.
โ ๐๐ฎ๐๐ถ๐ฒ๐ฟ ๐ง๐ฒ๐๐๐ถ๐ป๐ด
Composables are pure functions, making them easy to test in isolation.
๐๐ฎ๐๐ถ๐ฐ ๐๐ ๐ฎ๐บ๐ฝ๐น๐ฒ: ๐ ๐ฆ๐ถ๐บ๐ฝ๐น๐ฒ ๐๐ผ๐๐ป๐๐ฒ๐ฟ ๐๐ผ๐บ๐ฝ๐ผ๐๐ฎ๐ฏ๐น๐ฒ
๐๐๐ฒ๐๐ผ๐๐ป๐๐ฒ๐ฟ.๐ท๐
import { ref } from \'vue\';
export function useCounter() {
const count = ref(0);
const increment = () => count.value++;
const decrement = () => count.value--;
return { count, increment, decrement };
}
๐จ๐๐ถ๐ป๐ด ๐ถ๐ ๐ถ๐ป๐๐ถ๐ฑ๐ฒ ๐ฎ ๐ฐ๐ผ๐บ๐ฝ๐ผ๐ป๐ฒ๐ป๐:
<script setup>
import { useCounter } from \'@/composables/useCounter\';
const { count, increment, decrement } = useCounter();
</script>
This keeps logic reusable and clean.
๐๐๐ถ๐น๐ฑ๐ถ๐ป๐ด ๐ ๐ผ๐ฟ๐ฒ ๐ฃ๐ผ๐๐ฒ๐ฟ๐ณ๐๐น ๐๐ผ๐บ๐ฝ๐ผ๐๐ฎ๐ฏ๐น๐ฒ๐
Composables can contain:
- API calls
- Form validation
- Real-time listeners
- LocalStorage interactions
- Business rules
- Reusable animations
- Permission checks
Example: ๐๐๐ฒ๐๐ฒ๐๐ฐ๐ต.๐ท๐
import { ref } from \'vue\';
export function useFetch(url) {
const data = ref(null);
const loading = ref(true);
const load = async () => {
loading.value = true;
data.value = await fetch(url).then(res => res.json());
loading.value = false;
};
load();
return { data, loading, reload: load };
}
Now any component can fetch data without duplicating logic.
๐๐ฒ๐๐ ๐ฃ๐ฟ๐ฎ๐ฐ๐๐ถ๐ฐ๐ฒ๐ ๐ณ๐ผ๐ฟ ๐๐ผ๐บ๐ฝ๐ผ๐๐ฎ๐ฏ๐น๐ฒ๐
๐ญ. ๐ก๐ฎ๐บ๐ฒ ๐๐น๐ฒ๐ฎ๐ฟ๐น๐
useCart, useAuth, useDarkMode โ the name should describe the logicโs purpose.
๐ฎ. ๐๐ฒ๐ฒ๐ฝ ๐ง๐ต๐ฒ๐บ ๐๐ผ๐ฐ๐๐๐ฒ๐ฑ
Each composable should do one job, not everything.
๐ฏ. ๐ข๐ฟ๐ด๐ฎ๐ป๐ถ๐๐ฒ ๐ฏ๐ ๐๐ผ๐บ๐ฎ๐ถ๐ป
Create folders like:
/composables/auth
/composables/user
/composables/api
๐ฐ. ๐ฅ๐ฒ๐๐๐ฟ๐ป ๐ข๐ป๐น๐ ๐ช๐ต๐ฎ๐โ๐ ๐ก๐ฒ๐ฒ๐ฑ๐ฒ๐ฑ
Donโt overload components with unnecessary reactive values.
๐ฑ. ๐๐ผ๐ฐ๐๐บ๐ฒ๐ป๐ ๐ฌ๐ผ๐๐ฟ ๐๐ผ๐บ๐ฝ๐ผ๐๐ฎ๐ฏ๐น๐ฒ๐
If working in a team, simple comments help others understand whatโs inside.
๐๐ถ๐ป๐ฎ๐น ๐ง๐ต๐ผ๐๐ด๐ต๐๐
Composables are a game changer in Vue 3. They help you write cleaner code, reduce duplication, improve testing, and scale your architecture effortlessly. Once you start using composables, youโll find your components become shorter, clearer, and far easier to maintain.
If you are looking for any services related to Website Development, App Development, Digital Marketing and SEO, just email us at nchouksey@manifestinfotech.com
#Vuejs #Vue3 #Composables #CompositionAPI #ReusableLogic #WebDevelopment #JavaScript #FrontendDevelopment #VueTips #CleanCode #VueEcosystem #ModernVue #JSDeveloper #CodeReuse #WebAppDevelopment #DeveloperTips #ScalableArchitecture #VueCommunity #CodingBestPractices #ViteJs
