egoist / react-to-vue

Turn a React component into a Vue component.
MIT License
70 stars 5 forks source link

Vue 3 version #11

Closed IniZio closed 3 years ago

IniZio commented 3 years ago

My current approach

import { defineComponent, h, onBeforeUnmount, onMounted, onUpdated, ref } from 'vue'
import React from 'react'
import { render, unmountComponentAtNode } from 'react-dom'

export default (Component: any) => {
  return defineComponent({
    props: {
      defaultValue: {
        type: String,
        default: '',
      },
    },
    setup(props) {
      const elRef = ref()

      onMounted(() => {
        render(React.createElement(Component, props), elRef.value)
      })

      onUpdated(() => {
        render(React.createElement(Component, props), elRef.value)
      })

      onBeforeUnmount(() => {
        unmountComponentAtNode(elRef.value)
      })

      return () => h('div', { ref: elRef })
    },
  })
}
egoist commented 3 years ago

done, released 2.0

armenr commented 3 years ago

@egoist - this is great! Would you mind maybe sharing an example where you wrap a react component in a vue component, which gets rendered by a page/layout?

egoist commented 3 years ago

@armenr something like this? https://github.com/egoist/react-to-vue/blob/main/demo/main.tsx

armenr commented 3 years ago

@egoist - Thanks so much for the quick reply! I was thinking more like this -- ANY help (and I realize, I'm being an annoying user of your package, so thank you!) --> https://github.com/egoist/react-to-vue/issues/12