---
url: https://surgijs.github.io/surgijs/api.md
---
# @surgijs/core

## Reactivity

| Export | Signature |
| --- | --- |
| `createSignal` | `(value?) => [get, set]`; `set(v)` or `set(prev => next)` |
| `createMemo` | `(fn, init?) => get`; lazy, cached until a source changes |
| `createRenderEffect` | `(fn, init?)`; runs now and on changes, before effects |
| `createEffect` | `(fn, init?)`; runs on the next flush, after render effects |
| `createRoot` | `(fn: (dispose) => T, owner?) => T` |
| `onCleanup` | `(fn)`; runs when the current owner reruns or disposes |
| `onMount` | `(fn)`; runs once after the current render |
| `untrack` | `(fn) => T`; reads without subscribing |
| `batch` | `(fn) => T`; flushes once at the end |
| `flush` | `()`; runs queued memos, render effects and effects |
| `deferMount` | `(fn) => T`; collects effects created inside and starts them after `fn` returns |
| `getOwner` / `runWithOwner` | access and restore the current owner |
| `handleError` | `(owner, err)`; forwards to the nearest error boundary or throws |

## Hooks and context

Use hooks inside components and the `create*` primitives above outside them.

| Export | Signature |
| --- | --- |
| `useState` | `(value?) => [get, set]`, as `createSignal` |
| `useReducer` | `(reducer, init) => [state, dispatch]`; `state` is a getter |
| `useMemo` | `(fn, deps?) => get`; with `deps`, recomputes only when a listed dependency changes |
| `useEffect` | `(fn, deps?)`; after mount and on changes; `[]` runs once; a returned function is the cleanup |
| `useRef` | `() => { current: null }` |
| `useId` | `(prefix = 's') => string` |
| `createResource` | `(fetcher) => Resource` with `loading()` and `error()` |
| `createContext` | `(defaultValue) => { Provider, defaultValue }` |
| `useContext` | `(context) => get` |

## Components and control flow

| Export | Props |
| --- | --- |
| `defineComponent` | `(tag, render, schema?, { shadow?, form?, events? })` |
| `defineEvent` | `<T = void>({ bubbles?, composed?, cancelable? }?)` for `events` |
| `For` | `each`, `by?`, `children: (item, index) => JSX` |
| `Show` | `when`, `fallback?`, `children` (node or `(value) => JSX`) |
| `Switch` / `Match` | `fallback?` / `when`, `children` |
| `Portal` | `mount?`, `children` |
| `ErrorBoundary` | `fallback: JSX | (err, reset) => JSX`, `children` |

Types: `ReactiveProps<P>`, `Accessor`, `Setter`, `Signal`, `Resource`, `Ref`, `Schema`, `PropType`, `ComponentOptions`, `ComponentClass`, `EventDecl`, `EventOptions`, `Emit`, `FormHost`, `JSX`.

`deps` (type `Deps`) is `[a, b]` in compiled `.tsx`/`.jsx` files, which the compiler turns into `() => [a(), b()]`; elsewhere a function that reads the dependencies or an array of getters.

## `@surgijs/core/internal`

Compiled JSX imports these helpers from `@surgijs/core/internal`, so `@surgijs/core` holds only the application API. They are not covered by semver; import them only for hand-written integrations. Keep `@surgijs/compiler` and `@surgijs/core` on the same version: they are released together.

| Export | Purpose |
| --- | --- |
| `bind(node, read, apply, arg?)` | compiled binding, `read(arg)` supplies the value |
| `effect(fn, init?)` | render effect used by compiled code |
| `template(html, flags?)` | cloner for a static template; flag `1` imports the node, `2` parses inside `<svg>` |
| `insert(parent, value, marker?, current?)` | renders a value at a position |
| `setText`, `className`, `attr`, `attrNS`, `style`, `setProp`, `spread`, `use` | single DOM writes |
| `delegate(events)`, `delegateRoot(root)`, `undelegateRoot(root)` | event delegation |
| `createComponent(fn, props)` | calls a component untracked |
| `toNodes(value)` | resolves a value to a node list |
| `diff(parent, a, b)` | keyed reconciliation of two node lists |

## @surgijs/core/server

| Export | Purpose |
| --- | --- |
| `renderToString(tag, view, props?, options?)` | HTML with a declarative shadow root; `options.children` adds light DOM, `options.delegatesFocus` adds `shadowrootdelegatesfocus`; `options.styles` renders `<style data-s>`. Throws on an async part |
| `renderToStream(tag, view, props?, options?)` | the same markup as a `ReadableStream<string>`, sent in chunks as async parts resolve |
| `shadowHTML(tag, inner, attrs?, options?)` | host tag wrapper, same `options` |
| `escape(value)`, `attrPart(name, value)` | helpers used by SSR output |

## @surgijs/core/hydrate

| Export | Purpose |
| --- | --- |
| `enableHydration()` | components adopt existing shadow root content |
| `disableHydration()` | back to client rendering |
