Blog>
Snippets

Accessing the component instance from a VNode

Details how to access the component's instance from a VNode for extracting properties or emitting events.
// Assume 'h' is a function to create a VNode, such as the one in Vue.js
// Create a VNode for a component
const vnode = h(MyComponent);

// Access the component instance from the VNode
const componentInstance = vnode.component;

// Now you can access properties or methods from the component instance
const someProperty = componentInstance.props.someProperty;

// You can also emit events from the component instance
componentInstance.emit('myEvent');
This code demonstrates how to create a Virtual Node (VNode) for a component using a hypothetical 'h' function, which is similar to the render function in Vue.js, and how to access the component's instance from the VNode. It further shows how to extract properties and how to emit events from the component instance.