Skip to content

SurgiJS for React developers ​

SurgiJS keeps the parts of React you write every day (JSX, components as functions, hooks with the same names) and compiles them to web components with fine-grained updates. Most code reads the same. The differences come from one fact:

A component runs once. It sets up state and returns the DOM; after that, a change updates only the places that read it.

Side by side ​

ReactSurgiJS
function Counter({ step })const Counter = (props: ReactiveProps<{ step?: number }>), then const { step } = props
step is a valuestep is a getter: {step} in JSX, step() in code
const [count, setCount] = useState(0)same; count is a getter
count * 2count() * 2 (JSX and template literals call it for you)
useMemo(() => count * 2, [count])useMemo(() => count() * 2, [count])
useEffect(fn, [count])same
useEffect(fn, [])same: runs once after mount
useEffect(fn) (every render)useEffect(fn) tracks every signal fn reads and reruns when one changes
useCallback(fn, deps)a plain function: components do not re-run, so functions are stable
React.memo, key tricks to avoid rendersnot needed
useRef()same, ref={ref} on an element
useContext(Ctx)same, returns a getter
{items.map((i) => <Row key={i.id} … />)}<For each={items} by={(i) => i.id}>{(i) => <Row … />}</For>
{cond && <A />}<Show when={cond}><A /></Show>
classNameclass (or className)
onChange on inputsonInput for every keystroke, onChange on commit, as in the DOM
export default App + createRoot(el).render(<App />)defineComponent('my-app', App, schema) and <my-app> in HTML

Things that behave differently ​

The component body is not a render function. Code at the top of a component runs once, at mount. Reading a getter there takes a snapshot:

tsx
const Price = (props: ReactiveProps<{ amount: number }>) => {
  const { amount } = props;
  const label = `$${amount().toFixed(2)}`; // computed once, never updates
  const live = () => `$${amount().toFixed(2)}`; // a function updates where it is read
  return <span>{live}</span>;
};

Use useMemo or a plain arrow for derived values, as in live above.

Do not destructure into values. const { amount } = props keeps the getter. const amount = props.amount() reads it once. The same holds for objects: { count } in an object literal stores the current value, and the compiler warns about it.

Hooks have no rules of hooks. Since components run once, hooks can be called in conditions, loops and For rows. The order does not matter.

Effects run after mount and on change. There is no render phase to sync with, so useEffect covers what useLayoutEffect does in React; DOM refs are attached when it runs.

Props are attributes too. Each component is a custom element. Props declared in the schema map to attributes (maxItems ↔ max-items) and properties, so the element works from plain HTML and other frameworks.

Events. Inside a component, onClick and friends work as in React (delegated, stopPropagation() honored). To emit events from a component, declare them with defineEvent; see Components.

React hooks you will not need ​

They exist to cope with re-renders, concurrent rendering or React-specific plumbing. A component that runs once has none of those problems.

ReactWhyIn SurgiJS
useCallbackkeeps a function's identity across rendersa plain function; it is created once
useLayoutEffectruns after the DOM changes, before paintuseEffect: it runs when the element connects, with the DOM attached and before paint
useReducerstate changes through (state, action) => stateuseState and a function that calls the setter
useImperativeHandlewhat a parent gets through refmethods and properties live on the custom element itself
useSyncExternalStoresubscribe to an external sourcea signal set from a listener in useEffect, or fromStore from @surgijs/store/core
useInsertionEffectCSS-in-JS before layoutdefineComponent(..., { styles }), shared through adoptedStyleSheets
useTransition, useDeferredValuekeep input responsive during heavy rendersupdates are fine-grained and batched in a microtask; there is no render to defer
useEffectEventread fresh values in an effect without depending on themlist only what should trigger the effect in deps, or read with untrack(fn)
use + Suspenseread a promise during rendercreateResource with Show for loading and error states
useActionState, useFormStatus, useOptimisticforms and server actionsform-associated elements ({ form: true }); mutation in @surgijs/store for optimistic updates
useDebugValuelabel a custom hook in React DevToolshook values show up in React DevTools without it

Coming from Vue, Svelte or Angular ​

Vue's Composition API, Svelte 5 runes and Angular signals also run component setup once and track dependencies, so most of their concepts map one to one.

TaskReactSurgiJSVue 3Svelte 5Angular
StateuseStateuseStateref()$statesignal()
Derived valueuseMemouseMemocomputed()$derivedcomputed()
Effect, tracked automatically—useEffect(fn)watchEffect()$effecteffect()
Effect on listed dependenciesuseEffect(fn, deps)useEffect(fn, deps)watch(deps, fn)$effect + untrackeffect() + untracked()
After mountuseEffect(fn, [])useEffect(fn, [])onMountedonMountafterNextRender()
Cleanupfunction returned from useEffectfunction returned from useEffectonUnmounted, onWatcherCleanupfunction returned from $effectDestroyRef.onDestroy()
DOM referenceuseRefuseRefuseTemplateRef()bind:thisviewChild()
ContextcreateContext + useContextsameprovide / injectsetContext / getContextproviders + inject()
Async datause + SuspensecreateResourceasync setup + Suspense{#await}resource()
Read without tracking—untrack—untrackuntracked()
Listitems.map + key<For each by>v-for + :key{#each items as i (i.id)}@for (i of items; track i.id)
Conditioncond && <A /><Show when>v-if{#if}@if
Ship as a web componentwrapper libraryevery component is onedefineCustomElementcustomElement: true@angular/elements

Primitives ​

Hooks are thin wrappers over createSignal, createMemo and createEffect. Use those outside components: shared state in a module, stores, utilities. Inside components prefer the hooks; Reactivity has the details.

MIT License