Purposeful Functionality
This demonstrates the intended functionality of functional components, highlighting their efficiency. As stated in the documentation:
...we can designate components as functional, indicating they are stateless (lack reactive data)
Because functional components are essentially functions, their rendering process is cost-effective.
Clarification
The significance of this-- and why n
does not exhibit reactivity-- lies in n
lacking observable
properties and not being subjected to dependency management/watching. The absence of overhead from dependencies contributes to the performance boost seen with functional components. Choosing speed means sacrificing observability, representing a favorable tradeoff when it's unnecessary. Opting for a non-functional component becomes advisable if observability is required.
Next Steps
Your decision could involve using a traditional non-functional component or pondering the feasibility of dividing your functional component further to isolate reactive parts into a non-functional subcomponent.
Additional Considerations
If you manually introduce observability to your functional component, the desired behavior can be attained; however, leveraging a non-functional component would be more appropriate. Pay attention to the usage of observable
:
import Vue from 'vue';
let state = Vue.observable({n: 1});
function add() {
console.log(state.n)
return ++state.n
}
export default {
functional: true,
render(h, ctx) {
return (<div>
<h1>{state.n}</h1>
<button onClick={add}>click</button>
</div>)
}
}
(Note: While render
functions are common in normal components too, this information clarifies that functional components aren't obligatory for utilizing render
functions.)