( component: Component<P>, props?: CreateNativeViewProps<P>, contextOverrides?: ContextOverrides, )
| 22 | >; |
| 23 | |
| 24 | export function createNativeView<T = View, P = any>( |
| 25 | component: Component<P>, |
| 26 | props?: CreateNativeViewProps<P>, |
| 27 | contextOverrides?: ContextOverrides, |
| 28 | ) { |
| 29 | let isMounted = false; |
| 30 | let vm: ComponentPublicInstance | null; |
| 31 | let currentApp = renderer.createApp(component, props); |
| 32 | // Destructure so as not to copy over the root app instance |
| 33 | const { app, ...rootContext } = rootApp._context; |
| 34 | const context = { ...rootContext, ...contextOverrides }; |
| 35 | |
| 36 | type M = VNode<RendererNode, RendererElement, { nativeView: T }>; |
| 37 | return { |
| 38 | context, |
| 39 | get vnode() { |
| 40 | return vm?.$.vnode; |
| 41 | }, |
| 42 | get nativeView(): T { |
| 43 | return this.vnode?.el.nativeView; |
| 44 | }, |
| 45 | mount(root: NSVNode = new NSVRoot()) { |
| 46 | if (isMounted) { |
| 47 | return this.vnode as M; |
| 48 | } |
| 49 | |
| 50 | // Create a NEW app instance for remount (after HMR unmount) as Vue doesn't allow mounting the same app instance twice |
| 51 | if (!currentApp._instance) { |
| 52 | currentApp = renderer.createApp(component, props); |
| 53 | } |
| 54 | |
| 55 | Object.keys(context).forEach((key) => { |
| 56 | currentApp._context[key] = context[key]; |
| 57 | }); |
| 58 | |
| 59 | vm = currentApp.mount(root); |
| 60 | |
| 61 | // Set reload callback on the component instance's appContext, for HMR to work on navigated pages |
| 62 | if (context.reload && vm && (vm as any).$?.appContext) { |
| 63 | (vm as any).$.appContext.reload = context.reload; |
| 64 | } |
| 65 | |
| 66 | isMounted = true; |
| 67 | |
| 68 | return this.vnode as M; |
| 69 | }, |
| 70 | unmount() { |
| 71 | if (!isMounted) return; |
| 72 | |
| 73 | vm = null; |
| 74 | currentApp.unmount(); |
| 75 | |
| 76 | isMounted = false; |
| 77 | }, |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | export const ELEMENT_REF = Symbol(__DEV__ ? `elementRef` : ``); |
no outgoing calls