bfanger / svelte-preprocess-react

Seamlessly use React components inside a Svelte app
MIT License
126 stars 6 forks source link

How to: convert react component with multiple properties #38

Closed zeskysee closed 3 months ago

zeskysee commented 3 months ago

I had an app layout that look like this, may I know how to convert it to a svelte component so i could passed in the primary header, sidebar & main which is also a sveltify component?

<AppLayout
  header={
    <PrimaryHeader productName="name" />
  }
  sidebar={
    <Sidebar
      open={sidebarOpen}
      onToggle={setSidebarOpen}
      openContent={<div style={{ width: spacing192 }} />}
    />
  }
  main={<Outlet />}
/>
bfanger commented 3 months ago

In pure Svelte you'd write the AppLayout using named slots.

<AppLayout>
  <PrimaryHeader slot="header" productName="name" />
  ...

(And in Svelte 5 you'd use snippet props)

Named slots and snippets are not supported by sveltified React components.

You can pass React elements to sveltified React component as props in a svelte file, but you can't use JSX, so you'd need to write it as:

<script lang="ts">
  import { createElement } from "react"
</script>

<AppLayout header={createElement(PrimaryHeader, { productName: "name" })} 

But I recommend rewriting the AppLayout component to Svelte and using the slots approach

zeskysee commented 3 months ago

Thank you for your quick response