React / foldkit-react

react

Teams migrating incrementally between Foldkit and React—or adopting a specialized React widget like a rich editor or chart inside a Foldkit screen—need both frameworks to share one page without fighting over the same DOM subtree or duplicating state.

foldkit-react runs React components inside Foldkit views (ReactComponent.define) and embeds Foldkit programs inside React apps (FoldkitComponent.define). Each side keeps its own renderer and internal state; they meet at a single host element where only props, Messages (events sent to Foldkit's update), and Foldkit Ports cross the boundary. See both directions wired together in examples/react.

Install

sh
pnpm add foldkit-react foldkit effect react react-dom

React 19 is required.

Start with a React widget in Foldkit

React inside Foldkit

Wrap a React component with ReactComponent.define, list its event callback props, and render it from a Foldkit view:

ts
import { Schema } from 'effect'
import type { Html, HtmlBuilder } from 'foldkit/html'
import { defineMessageUnion } from 'foldkit/message'
import { ReactComponent } from 'foldkit-react'
import { createElement } from 'react'

const Button = (props: { readonly label: string; readonly onPress?: () => void }) =>
  createElement('button', { onClick: props.onPress }, props.label)
const ReactButton = ReactComponent.define(Button, { events: ['onPress'] })
const Model = Schema.Struct({ count: Schema.Number })
type Model = typeof Model.Type
const Message = defineMessageUnion({ Incremented: {} })
type Message = typeof Message.Type

const update = (model: Model, _message: Message) => ({ model: { count: model.count + 1 } })
const view = (model: Model, h: HtmlBuilder<Message>): Html =>
  ReactButton.view(
    { props: { label: `Count: ${model.count}` }, messages: { onPress: () => Message.Incremented() } },
    h,
  )

Rendered in a Foldkit program initialized at { count: 0 }, ReactButton.view mounts a <foldkit-react-host> element containing the React button. Clicking it calls onPress, dispatches Message.Incremented(), increments count in update, and re-renders the same React root with "Count: 1" without remounting.

Ownership stays cleanly separated across both directions:

  • React inside Foldkit: Your Foldkit Model owns prop values; React owns internal widget state (useState, focus, library selections) inside <foldkit-react-host>. Events return void callbacks (callbacks returning values such as shouldClose: () => boolean stay ordinary props) and reach update through messages.
  • Foldkit inside React: The embedded Foldkit program owns its Model; React communicates exclusively through inbound and outbound Foldkit Ports.

Add Suspense and error boundaries

Each React island owns its own React root, so lazy, use(promise), and suspending libraries show suspenseFallback while waiting, and errorBoundary catches render failures and optionally emits a Foldkit Message (in the snippet below, model and h come from your Foldkit view, and Editor / Spinner are your React components):

ts
const ReactEditor = ReactComponent.define(Editor)

ReactEditor.view(
  {
    props: { document: model.document },
    suspenseFallback: createElement(Spinner),
    errorBoundary: {
      fallback: () => createElement('p', null, 'The editor crashed.'),
      toMessage: error => EditorCrashed({ reason: String(error) }),
    },
  },
  h,
)

Without errorBoundary, render errors remove the island root and propagate normally; with it, fallback stays mounted until the island unmounts.

Suspend on AsyncData with readAsyncData

When Foldkit Commands manage loading via AsyncData in your Model, readAsyncData lets a React island read that value directly through Suspense and errorBoundary:

ts
const Profile = ({ user }: { readonly user: AsyncData.AsyncData<User, string> }) => {
  const { data, isRefreshing } = readAsyncData(user)
  return createElement('p', null, isRefreshing ? `${data.name} (refreshing)` : data.name)
}
const ReactProfile = ReactComponent.define(Profile)

ReactProfile.view(
  {
    props: { user: model.user },
    suspenseFallback: createElement(Spinner),
    errorBoundary: {
      fallback: error => (error instanceof AsyncDataFailure ? `Not found: ${error.error}` : 'Crashed'),
    },
  },
  h,
)
AsyncData statereadAsyncData behavior
Initial / LoadingSuspends to suspenseFallback
Failure(error)Throws AsyncDataFailure to errorBoundary
Success(data)Returns { data }
Refreshing(data)Returns { data, isRefreshing: true }
Stale(error, data)Returns { data, maybeStaleError: Some(error) }

Pass AsyncData values through island props or React state (such as outbound Ports) so updates retry suspended components automatically.

Style and key the host element

Pass hostAttributes to attach Foldkit keys, classes, or ARIA attributes onto <foldkit-react-host> (which defaults to display: inline):

ts
ReactCounter.view({ props: { id: row.id }, hostAttributes: [h.Key(row.id), h.Class('row')] }, h)

Keyed moves keep the React tree mounted; removing the island unmounts React and runs effect cleanups. Under Foldkit DevTools time travel, jumping to past Models updates island props while leaving React's internal state alone.