Discover how tiny, fast components can make your Vue app feel lightning quick!
Why Functional components in Vue? - Purpose & Use Cases
Imagine building a Vue app where every small UI part needs its own component, but you write full component code with data, methods, and lifecycle hooks even for simple display parts.
This approach makes your code bulky and slow because Vue has to manage extra overhead like reactivity and lifecycle for components that just show static content or simple props.
Functional components let you write lightweight, stateless components that only render UI based on props, making your app faster and your code cleaner.
<template><div>{{ message }}</div></template>
<script>export default { props: ['message'], data() { return {}; }, methods: {}, mounted() {} }</script>export default {
functional: true,
props: ['message'],
render(h, { props }) {
return h('div', props.message)
}
}It enables building fast, simple UI pieces without unnecessary overhead, improving app performance and developer productivity.
Think of a button component that only shows a label and triggers a click. Using a functional component makes it super light and quick to render everywhere.
Functional components are simple, stateless, and faster.
They reduce code size by skipping lifecycle and reactivity.
Perfect for small UI parts that just display props.
Practice
functional component in Vue?Solution
Step 1: Understand functional component basics
Functional components in Vue are designed to be simple and stateless, meaning they do not hold reactive data or manage lifecycle hooks.Step 2: Compare options with definition
It is stateless and has no reactive data. correctly states that functional components are stateless. Options A, C, and D describe features not applicable to functional components.Final Answer:
It is stateless and has no reactive data. -> Option DQuick Check:
Functional component = stateless [OK]
- Thinking functional components have reactive state
- Assuming they use lifecycle hooks
- Confusing with class-based components
<script setup> syntax in Vue 3?Solution
Step 1: Recall Vue 3 functional component syntax
In Vue 3 with<script setup>, functional components can be defined as functions returning VNodes using thehhelper.Step 2: Analyze each option
<script setup>\nimport { h } from 'vue'\nconst MyFuncComp = (props) => h('div', props.text)\n</script>correctly defines a functional component returning a div with text usingh.<script setup>\nconst MyFuncComp = (props) => <div>{props.text}</div>\n</script>uses JSX-like syntax which is not default in Vue. Options B and C use options API syntax incorrectly inside<script setup>.Final Answer:
<script setup> import { h } from 'vue' const MyFuncComp = (props) => h('div', props.text) </script> -> Option BQuick Check:
Functional component = function returning VNode with h() [OK]
- Using options API inside
- Trying to use JSX without setup
- Missing h() helper for rendering
<script setup>
import { h } from 'vue'
const Greeting = (props) => h('p', `Hello, ${props.name}!`)
</script>
<template>
<Greeting name="Alice" />
</template>Solution
Step 1: Understand the functional component output
The component Greeting is a function that returns a paragraph element with text including the passed propname.Step 2: Substitute the prop value
When used withname="Alice", it renders <p>Hello, Alice!</p> as a normal HTML element.Final Answer:
<p>Hello, Alice!</p> -> Option CQuick Check:
Functional component renders HTML with props substituted [OK]
- Expecting JSX syntax output
- Thinking it returns raw component tag
- Assuming template is required
<script setup>
const Button = (props) => {
return <button>{props.label}</button>
}
</script>Solution
Step 1: Check syntax used in functional component
The code uses JSX-like syntax (<button>{props.label}</button>) inside a function without JSX support configured.Step 2: Understand Vue default rendering
Vue requires usingh()helper or templates unless JSX is set up. Without JSX setup, this causes a syntax error.Final Answer:
Using JSX syntax without proper setup causes an error. -> Option AQuick Check:
JSX needs setup; otherwise use h() or template [OK]
- Assuming JSX works by default
- Forgetting to import h()
- Thinking return statement is disallowed
<script setup> and the h function?Solution
Step 1: Understand how to render lists in functional components
Using thehfunction, you can create an unordered list with list items by mapping over the items array and creatinglielements.Step 2: Analyze each option
<script setup> import { h } from 'vue' const ItemList = (props) => h('ul', props.items.map(item => h('li', item))) </script>correctly maps each item to anlielement inside aul.<script setup> const ItemList = { functional: true, props: ['items'], template: '<ul><li v-for="item in items">{{ item }}</li></ul>' } </script>uses options API incorrectly inside<script setup>.<script setup> const ItemList = (props) => { return <ul>{props.items.map(item => <li>{item}</li>)} uses JSX without setup.<script setup> import { h } from 'vue' const ItemList = (props) => h('ul', [h('li', props.items)]) </script>incorrectly passes the whole array as a singleli.Final Answer:
<script setup> import { h } from 'vue' const ItemList = (props) => h('ul', props.items.map(item => h('li', item))) </script> -> Option AQuick Check:
Map items to li elements using h() inside ul [OK]
- Passing whole array as one li
- Using template syntax inside script setup
- Using JSX without setup
Ready to prove it?
Pass the test and this pattern moves unknown
