diff --git a/dev-project-react/package.json b/dev-project-react/package.json index 0eb14b9..cef8afc 100644 --- a/dev-project-react/package.json +++ b/dev-project-react/package.json @@ -54,7 +54,7 @@ "style-loader": "^3.3.1", "tailwindcss": "^3.0.2", "terser-webpack-plugin": "^5.2.5", - "veaury": "^2.3.1-beta.0", + "veaury": "^2.3.2-beta.0", "vue": "^3.2.31", "vue-router": "^4.0.16", "vuex": "^4.0.2", diff --git a/dev-project-vue3/package.json b/dev-project-vue3/package.json index 87994e9..a4cfd9e 100644 --- a/dev-project-vue3/package.json +++ b/dev-project-vue3/package.json @@ -11,9 +11,9 @@ "babel-plugin-transform-react-jsx": "^6.24.1", "babel-preset-react-app": "^10.0.1", "core-js": "^3.6.5", - "react": "^18.1.0", - "react-dom": "^18.1.0", - "veaury": "^2.3.0-beta.1", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "veaury": "^2.3.2-beta.0", "vue": "^3.0.0", "vue-router": "^4.0.12", "vuex": "^4.0.2" diff --git a/package.json b/package.json index 0676962..de7e687 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "veaury", "private": false, - "version": "2.3.1", + "version": "2.3.2", "description": "Use React in Vue3 and Vue3 in React, And as perfect as possible!", "main": "dist/veaury.umd.js", "module": "dist/veaury.esm.js", diff --git a/src/applyReactInVue.js b/src/applyReactInVue.js index a5f8f02..0598dea 100644 --- a/src/applyReactInVue.js +++ b/src/applyReactInVue.js @@ -1,12 +1,20 @@ import * as React from "react" -import * as ReactDOM from "react-dom" -// TODO: react 18 -// import { createRoot } from "react-dom/client" +import { version } from "react" import applyVueInReact from "./applyVueInReact" import { setOptions } from "./options" import { h as createElement, getCurrentInstance, reactive, Fragment as VueFragment, Comment } from 'vue' import { overwriteDomMethods, recoverDomMethods } from './overrideDom' +const ReactMajorVersion = parseInt(version) +let ReactDOM; +// TODO: May be optimized in the future +// Not a good way to judge, because this will result in the exported esm format with `require`. +try { + ReactDOM = require('react-dom/client') +} catch(e) { + ReactDOM = require('react-dom') +} + function toRaws(obj) { return obj; // TODO: Use toRaw to transform property values passed to react components @@ -562,13 +570,16 @@ export default function applyReactInVue(component, options = {}) { return } + if (ReactMajorVersion > 17) { + this.__veauryReactApp__ = ReactDOM.createRoot(container) + this.__veauryReactApp__.render(reactRootComponent) + return + } ReactDOM.render( reactRootComponent, container, ) - // TODO: react 18 - // this.__veauryReactApp__ = createRoot(container) - // this.__veauryReactApp__.render(reactRootComponent) + } else { const setReactState = () => { @@ -655,9 +666,12 @@ export default function applyReactInVue(component, options = {}) { // Override some methods of native lookup 'dom' overwriteDomMethods(this.$refs.react) // Destroy the React root node - ReactDOM.unmountComponentAtNode(this.$refs.react) - // TODO: react 18 - // this.__veauryReactApp__.unmount() + if (ReactMajorVersion > 17) { + this.__veauryReactApp__.unmount() + } else { + ReactDOM.unmountComponentAtNode(this.$refs.react) + } + // restore native method recoverDomMethods() }, diff --git a/vite/index.js b/vite/index.js index b963112..b387af7 100644 --- a/vite/index.js +++ b/vite/index.js @@ -27,6 +27,16 @@ function veauryVitePlugins({type, vueJsxInclude, vueJsxExclude}) { ...vueJsx(vueJsxOptions), enforce: 'pre' }, + // recover esbuild include + { + config(){ + return { + esbuild: { + include: /\.tsx*$/ + } + } + } + }, react() ] }