Reactive Component Render Function
Create a render function within a component and make use of reactive data to control what gets returned from the function.
// Define a simple component with a reactive state
const MyComponent = {
data() {
return {
// Initialize a reactive property
count: 0
};
},
// Define a render function that reacts to changes in the 'count' property
render(h) {
// Create a button that will increment the count when clicked
const button = h('button', {
on: {
click: () => {
this.count++; // increment the count, triggering reactivity
}
}
}, 'Increment');
// Display the current count in a paragraph
const paragraph = h('p', {}, `Current count: ${this.count}`);
// Return an array of virtual DOM nodes to render
return [paragraph, button];
}
};
This example defines a simple Vue component with a reactive data property that counts the number of times a button is clicked. The render function uses the 'h' function to create a button element and a paragraph element. The button has an event listener that increments the count. The paragraph displays the current count. Both elements are returned as part of an array of virtual DOM nodes, and they are re-rendered whenever the count changes.