---
title: Use withDirectives for Custom Directives in Render Functions
impact: LOW
impactDescription: Custom directives require special handling with withDirectives helper
type: best-practice
tags: [vue3, render-function, directives, custom-directive]
---
# Use withDirectives for Custom Directives in Render Functions
**Impact: LOW** - To apply custom directives to vnodes in render functions, use the `withDirectives` helper. Attempting to apply directives through props or other means will not work.
The `withDirectives` function wraps a vnode and applies directives with their values, arguments, and modifiers.
## Task Checklist
- [ ] Import `withDirectives` from 'vue' when using custom directives
- [ ] Import or define the directive object
- [ ] Pass directive as array: `[directive, value, argument, modifiers]`
- [ ] Multiple directives can be applied at once
**Incorrect:**
```javascript
import { h } from 'vue'
const vFocus = { mounted: (el) => el.focus() }
export default {
setup() {
return () => {
// WRONG: Directives don't work as props
return h('input', {
'v-focus': true // This doesn't work
})
}
}
}
```
**Correct:**
```javascript
import { h, withDirectives } from 'vue'
// Custom directive
const vFocus = {
mounted: (el) => el.focus()
}
export default {
setup() {
return () => {
// CORRECT: Use withDirectives
return withDirectives(
h('input'),
[[vFocus]] // Array of directive tuples
)
}
}
}
```
## Directive with Value, Argument, and Modifiers
The directive tuple format: `[directive, value, argument, modifiers]`
```javascript
import { h, withDirectives, resolveDirective } from 'vue'
// Directive definition
// Usage in template: