--- title: Know When to Use Directives vs Components impact: MEDIUM impactDescription: Using directives when components are more appropriate leads to harder maintenance and testing type: best-practice tags: [vue3, directives, components, architecture, best-practices] --- # Know When to Use Directives vs Components **Impact: MEDIUM** - Accessing the component instance from within a custom directive is often a sign that the directive should rather be a component itself. Directives are designed for low-level DOM manipulation, while components are better for encapsulating behavior that involves state, reactivity, or complex logic. Choosing the wrong abstraction leads to code that's harder to maintain, test, and reuse. ## Task Checklist - [ ] Use directives for simple, stateless DOM manipulations - [ ] Use components when you need encapsulated state or complex logic - [ ] If accessing `binding.instance` frequently, consider using a component instead - [ ] If the behavior needs its own template, use a component - [ ] Consider composables for stateful logic that doesn't need a template ## Decision Matrix | Requirement | Use Directive | Use Component | Use Composable | |-------------|--------------|---------------|----------------| | DOM manipulation only | Yes | - | - | | Needs own template | - | Yes | - | | Encapsulated state | - | Yes | Maybe | | Reusable behavior | Yes | Yes | Yes | | Access to parent instance | Avoid | - | Yes | | SSR support needed | Avoid | Yes | Yes | | Third-party lib integration | Yes | - | Maybe | | Complex reactive logic | - | Yes | Yes | ## Directive-Appropriate Use Cases ```javascript // GOOD: Simple DOM manipulation const vFocus = { mounted: (el) => el.focus() } // GOOD: Third-party library integration const vTippy = { mounted(el, binding) { el._tippy = tippy(el, binding.value) }, updated(el, binding) { el._tippy?.setProps(binding.value) }, unmounted(el) { el._tippy?.destroy() } } // GOOD: Event handling that Vue doesn't provide const vClickOutside = { mounted(el, binding) { el._handler = (e) => { if (!el.contains(e.target)) binding.value(e) } document.addEventListener('click', el._handler) }, unmounted(el) { document.removeEventListener('click', el._handler) } } // GOOD: Intersection Observer const vLazyLoad = { mounted(el, binding) { const observer = new IntersectionObserver(([entry]) => { if (entry.isIntersecting) { el.src = binding.value observer.disconnect() } }) observer.observe(el) el._observer = observer }, unmounted(el) { el._observer?.disconnect() } } ``` ## Component-Appropriate Use Cases ```vue ``` ```vue ``` ## Composable-Appropriate Use Cases ```javascript // GOOD: Reusable stateful logic without template // useClickOutside.js import { onMounted, onUnmounted, ref } from 'vue' export function useClickOutside(elementRef, callback) { const isClickedOutside = ref(false) const handler = (e) => { if (elementRef.value && !elementRef.value.contains(e.target)) { isClickedOutside.value = true callback?.(e) } } onMounted(() => document.addEventListener('click', handler)) onUnmounted(() => document.removeEventListener('click', handler)) return { isClickedOutside } } // Usage in component const dropdownRef = ref(null) const { isClickedOutside } = useClickOutside(dropdownRef, () => { isOpen.value = false }) ``` ## Anti-Pattern: Directive Accessing Instance Too Much ```javascript // ANTI-PATTERN: Directive relying heavily on component instance const vBadPattern = { mounted(el, binding) { // Accessing instance too much = should be a component const instance = binding.instance instance.someMethod() instance.someProperty = 'value' instance.$watch('someProp', (val) => { el.textContent = val }) } } // BETTER: Use a component or composable // Component version ``` ## When Instance Access is Acceptable ```javascript // OK: Minimal instance access for specific needs const vPermission = { mounted(el, binding) { // Checking a global permission - acceptable const userPermissions = binding.instance.$store?.state.user.permissions if (!userPermissions?.includes(binding.value)) { el.style.display = 'none' } } } ``` ## Reference - [Vue.js Custom Directives](https://vuejs.org/guide/reusability/custom-directives) - [Vue.js Composables](https://vuejs.org/guide/reusability/composables.html) - [Vue.js Components Basics](https://vuejs.org/guide/essentials/component-basics.html)