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)依賴
$ 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項(xiàng)目動(dòng)態(tài)修改主題顏色的方案
這篇文章主要介紹了React項(xiàng)目動(dòng)態(tài)修改主題顏色的方案,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-01-01
教你快速搭建 React Native 開(kāi)發(fā)環(huán)境
這篇文章主要介紹了搭建 React Native 開(kāi)發(fā)環(huán)境的詳細(xì)過(guò)程,本文通過(guò)圖文指令給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
適用于React?Native?旋轉(zhuǎn)木馬應(yīng)用程序介紹
這篇文章主要介紹了適用于React?Native?旋轉(zhuǎn)木馬應(yīng)用程序介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
詳解React Angular Vue三大前端技術(shù)
當(dāng)前世界中,技術(shù)發(fā)展非常迅速并且變化迅速,開(kāi)發(fā)者需要更多的開(kāi)發(fā)工具來(lái)解決不同的問(wèn)題。本文就對(duì)于當(dāng)下主流的前端開(kāi)發(fā)技術(shù)React、Vue、Angular這三個(gè)框架做個(gè)相對(duì)詳盡的探究,目的是為了解開(kāi)這些前端技術(shù)的面紗,看看各自的廬山真面目。2021-05-05
React實(shí)現(xiàn)PDF預(yù)覽功能與終極優(yōu)化
在前端開(kāi)發(fā)中,PDF 預(yù)覽是個(gè)常見(jiàn)需求,本文主要來(lái)帶大家認(rèn)識(shí)一個(gè)基于 react-pdf 的自定義 PDF 預(yù)覽組件 PDFView,感興趣的小伙伴可以了解下2025-05-05
React Router 5.1.0使用useHistory做頁(yè)面跳轉(zhuǎn)導(dǎo)航的實(shí)現(xiàn)
本文主要介紹了React Router 5.1.0使用useHistory做頁(yè)面跳轉(zhuǎn)導(dǎo)航的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
如何在React?Native開(kāi)發(fā)中防止滑動(dòng)過(guò)程中的誤觸
在使用React?Native開(kāi)發(fā)的時(shí),當(dāng)我們快速滑動(dòng)應(yīng)用的時(shí)候,可能會(huì)出現(xiàn)誤觸,導(dǎo)致我們會(huì)點(diǎn)擊到頁(yè)面中的某一些點(diǎn)擊事件,誤觸導(dǎo)致頁(yè)面元素響應(yīng)從而進(jìn)行其他操作,表現(xiàn)出非常不好的用戶體驗(yàn)。2023-05-05

