Created
June 22, 2019 20:00
-
-
Save bnjm/d3cb865a5ed87bf1c4f40d5eba4b4b34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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