svelte5中使用react組件的方法
svelte5中使用react組件
在svelet5中導(dǎo)入并使用react組件庫(kù), 示例項(xiàng)目地址:https://github.com/shenshouer/my-svelte-react
在svelte5中當(dāng)前還有問(wèn)題,無(wú)法將children傳遞到react中渲染
使用svletkit創(chuàng)建項(xiàng)目
$ npx sv create my-svelte-react % npx sv create my-svelte-react ┌ Welcome to the Svelte CLI! (v0.6.10) │ ◇ Which template would you like? │ SvelteKit minimal │ ◇ Add type checking with Typescript? │ Yes, using Typescript syntax │ ◆ Project created │ ◇ What would you like to add to your project? (use arrow keys / space bar) │ none │ ◇ Which package manager do you want to install dependencies with? │ pnpm │ ◆ Successfully installed dependencies │ ◇ Project next steps ─────────────────────────────────────────────────────╮ │ │ │ 1: cd my-svelte-react │ │ 2: git init && git add -A && git commit -m "Initial commit" (optional) │ │ 3: pnpm run dev --open │ │ │ │ To close the dev server, hit Ctrl-C │ │ │ │ Stuck? Visit us at https://svelte.dev/chat │ │ │ ├──────────────────────────────────────────────────────────────────────────╯ │ └ You're all set! $ cd my-svelte-react $ pnpm install $ pnpm dev
安裝react相關(guān)依賴(lài)
$ pnpm i react react-dom $ pnpm i --save-dev @types/react @types/react-dom $ pnpm add @vitejs/plugin-react -D
修改vite.config.ts增加react支持
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react'; # <---- here
export default defineConfig({
plugins: [sveltekit(), react()] # <---- here
});創(chuàng)建react svelte適配器ReactAdapter.svelte, 代碼如下:
# src/lib/utils/ReactAdapter.svelte
<script lang="ts">
import React from "react";
import ReactDOM from "react-dom/client";
import { onDestroy, onMount } from "svelte";
const e = React.createElement;
let container: HTMLElement;
let root: ReactDOM.Root;
onMount(() => {
const { el, children, class: _, ...props } = $$props;
try {
root = ReactDOM.createRoot(container);
root.render(e(el, props, children));
} catch (err) {
console.warn(`react-adapter failed to mount.`, { err });
}
});
onDestroy(() => {
try {
if (root) {
root.unmount();
}
} catch (err) {
console.warn(`react-adapter failed to unmount.`, { err });
}
});
</script>
<div bind:this={container} class={$$props.class}></div>目前此部分適配器有問(wèn)題, children無(wú)法獲取并且在react組件中渲染
添加react組件庫(kù), 如 ant design
$ pnpm add antd
# +page.svelte
<script lang="ts">
import { Button } from "antd";
import ReactAdapter from "$lib/utils/ReactAdapter.svelte";
</script>
<ReactAdapter el={Button} type="primary">Hello, World!</ReactAdapter>到此這篇關(guān)于svelte5中使用react組件的文章就介紹到這了,更多相關(guān)svelte5使用react組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用React實(shí)現(xiàn)一個(gè)完整的TodoList的示例代碼
本篇文章主要介紹了用React實(shí)現(xiàn)一個(gè)完整的TodoList的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
30行代碼實(shí)現(xiàn)React雙向綁定hook的示例代碼
本文主要介紹了30行代碼實(shí)現(xiàn)React雙向綁定hook的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
深入理解React Native原生模塊與JS模塊通信的幾種方式
本篇文章主要介紹了深入理解React Native原生模塊與JS模塊通信的幾種方式,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07

