Skip to content

Instantly share code, notes, and snippets.

@bnjm
Created June 22, 2019 20:00
Show Gist options
  • Save bnjm/d3cb865a5ed87bf1c4f40d5eba4b4b34 to your computer and use it in GitHub Desktop.
Save bnjm/d3cb865a5ed87bf1c4f40d5eba4b4b34 to your computer and use it in GitHub Desktop.
import Vue from 'vue'
import React, { useRef, useEffect, useCallback } from 'react'
const getWrappedVueComponent = component => props => {
const container = useRef()
const instance = useRef()
useEffect(() => {
instance.current = new Vue({ ...component, el: container.current })
}, [])
useEffect(() => {
// Object.assign will trigger the instance setters and update the component
Object.assign(instance.current, props)
}, [props])
return <div ref={container} />
}
const useVueComponent = component => {
// useCallback should prevent returning a new component type each render
return useCallback(getWrappedVueComponent(component), [])
}
export { useVueComponent, getWrappedVueComponent }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment