Created
June 14, 2024 13:16
-
-
Save mquandalle/709e2ba691aa6dc6086a40df88bd4f1b to your computer and use it in GitHub Desktop.
Use a React component in a Svelte 5 component
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
<script> | |
import { createElement } from "react"; | |
import { createRoot } from "react-dom/client"; | |
import { onMount, onDestroy } from "svelte"; | |
let container = $state(); | |
let reactRoot = $state(); | |
const { el, children, class: classNames, ...props } = $props(); | |
onMount(() => { | |
reactRoot = createRoot(container); | |
}); | |
$effect(() => { | |
reactRoot.render(createElement(el, props, children)); | |
}); | |
onDestroy(() => { | |
reactRoot?.unmount(); | |
}); | |
</script> | |
<div bind:this={container} class={classNames}></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage