Coming from React
If you know React, you already know most of What. This page covers the key differences so you can be productive immediately.
To server-render a React-compat app, import from react-dom/server and call hydrateRoot from react-dom/client in the browser. The reactCompat() Vite plugin maps those imports to what-react. Use the same identifierPrefix on both sides to keep useId() stable. For translate mode, use the matching translated server build and compile the browser build with what({ hydratable: true }) when Vite does not enable hydratable output automatically.
The Big Difference: No Re-Renders
In React, when state changes, the entire component function re-runs. Every variable is recalculated, every expression is re-evaluated, and React diffs a tree of objects to figure out what changed in the DOM.
In What, component functions run once. Signals update the specific DOM nodes that depend on them: no intermediate representation, no diffing, no re-running the whole function. The compiler transforms JSX into direct DOM operations at build time.
This is faster, but it means some React patterns don't translate directly.
How JSX Compiles
In React, JSX compiles to React.createElement() calls that build an object tree, which React diffs against the previous tree. In What, the compiler extracts static HTML into cloneable templates and wraps dynamic expressions in fine-grained effects:
JSX -> compiler -> template() + insert() + effect() -> DOM
Static parts are never recreated. Only the specific text nodes or attributes that depend on a signal are updated when that signal changes.
React's Hooks, in a Component That Runs Once
The hooks keep React's names and call shapes: const [count, setCount] = useState(0), useReducer(reducer, initial), useMemo(fn), useEffect(fn, deps) returning a cleanup, useRef(initial).current, useContext(Ctx), useId() (a string). What differs is that a component runs once, so a hook cannot hand you this render's value. It hands you the signal:
useStateanduseReducerreturn[accessor, setter].countis a function: read the value by calling it,count()orstate().items.setCount(5)andsetCount((c) => c + 1)write, as in React.useMemo(fn)returns an accessor too (total()), recomputed when a signalfnreads changes. Its dependency array is ignored, and so isuseCallback's, which returns your function unchanged.useEffect(fn)runs after the component mounts and re-runs when a signal it reads changes; the cleanup it returns runs before the next run and on unmount. With a dependency array it re-runs only for the accessors listed ([count], never[count()]: a plain value reportsERR_EFFECT_DEP_NOT_FUNCTION), and[]runs it once.useContext(Ctx)returns what the nearest provider passed. Provide a signal for a value that changes (<Theme value={theme}>) and call it where you read it.- The rules that exist because React re-renders do not apply: no stale closures (a handler that calls
count()reads the current value), no exhaustive dependency lists, nothing to memoize for a stable identity. Hooks still run only in a component body.
The habit that makes React-trained code correct: whatever a hook or signal() returns, call it to read it. In development, a signal used as a value without the call (`${count}`, count + 1) reports ERR_MISSING_SIGNAL_READ.
import { mount, useState, useEffect, useMemo } from 'what-framework';
function Counter() {
const [count, setCount] = useState(0);
const doubled = useMemo(() => count() * 2);
useEffect(() => {
document.title = `Count: ${count()}`;
});
return <button onClick={() => setCount((c) => c + 1)}>{() => `${count()} doubled is ${doubled()}`}</button>;
}
mount(<Counter />, '#app');
State: useState vs useSignal
What's useState hands you the signal's accessor and its setter. What also ships useSignal, which hands you the signal itself: you read it the same way and write it with .set(). The rest of this page uses useSignal; with useState the samples read the same and write through the setter. The canonical API is plain signal(), which works everywhere and returns the same object as useSignal; reach for it once the hook habit fades.
| React | What: useState |
What: useSignal |
|---|---|---|
const [x, setX] = useState(0) |
const [x, setX] = useState(0) |
const x = useSignal(0) |
x (read) |
x() (read, call it like a function) |
x() |
setX(5) |
setX(5) |
x.set(5) |
setX(prev => prev + 1) |
setX(prev => prev + 1) |
x.set(prev => prev + 1) |
In React:
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
}
In What, with either hook:
import { useState, useSignal } from 'what-framework';
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(c => c + 1)}>{count()}</button>;
}
function SignalCounter() {
const count = useSignal(0);
return <button onClick={() => count.set(c => c + 1)}>{count()}</button>;
}
Derived Values: Why useComputed Exists
This is the #1 thing that trips up React developers.
In React, const doubled = count * 2 just works because the whole function re-runs on every state change: doubled gets recalculated every time.
In What, the component function runs once. So const doubled = count() * 2 evaluates to a plain number at creation time and never updates:
// BROKEN: doubled is a dead number, computed once, never updates
function App() {
const count = useSignal(0);
const doubled = count() * 2; // Evaluates to 0, stays 0 forever
return <p>Doubled: {doubled}</p>; // Always shows "Doubled: 0"
}
Use useComputed to create a derived signal that tracks its dependencies and updates automatically:
// CORRECT: useComputed tracks count and re-derives when it changes
function App() {
const count = useSignal(0);
const doubled = useComputed(() => count() * 2);
return <p>Doubled: {doubled()}</p>; // Updates when count changes
}
When do you need useComputed?
If you're computing a value from signals and need to use it in multiple places or pass it to child components, use useComputed. It caches the result and only recomputes when dependencies change.
For simple expressions used only once in JSX, the compiler handles reactivity automatically: {count() * 2} works directly in JSX because what-compiler wraps it in an effect. That is the default setup create-what scaffolds, and it is what every sample on this page assumes. Without the compiler (the buildless full-stack template, which uses h() rather than JSX) the same expression is evaluated once and never updates, so write it as a thunk instead: h('p', {}, () => count() * 2).
The rule
| React pattern | What equivalent |
|---|---|
const x = a + b |
const x = useComputed(() => a() + b()) |
useMemo(() => expensive(), [dep]) |
useComputed(() => expensive(dep())), or useMemo(() => expensive(dep())): both return an accessor, and useMemo ignores a dependency array |
{count * 2} in JSX |
{count() * 2} in JSX (compiler handles it) |
Writing a Signal in the Component Body
In React, calling setState during render is bad practice but React catches it and shows a warning. What has no render pass to re-enter: the component body runs exactly once, inside untrack(), so a write there cannot re-trigger the component. It is not an error and it is not a loop, it is just a slower way of writing the initial value:
// REDUNDANT, not broken. The body runs once, so this renders 1
function Redundant() {
const count = useSignal(0);
count.set(c => c + 1); // Runs once. No re-render, no loop.
return <p>{count()}</p>;
}
Give the signal the value you actually want (useSignal(1)) and keep the body free of writes, so the component reads as a description of its starting state. Signal writes that depend on something belong in one of these places:
// 1. Event handlers
<button onClick={() => count.set(c => c + 1)}>+1</button>
// 2. Effects (for reactive side effects)
effect(() => {
if (query() && query().length > 2) {
fetchResults(query());
}
});
// 3. onMount (for one-time initialization)
onMount(() => {
count.set(parseInt(localStorage.getItem('count')) || 0);
});
Effects: Auto-Tracking vs Dependency Arrays
React effects require you to list dependencies manually:
// React: you manage the deps array
useEffect(() => {
document.title = `Count: ${count}`;
}, [count]); // Must list count or it won't re-run
What effects auto-track: any signal read inside the effect becomes a dependency automatically:
// What: no deps array needed
effect(() => {
document.title = `Count: ${count()}`;
// Automatically re-runs when count changes
// No dependency array, tracking is automatic
});
What about useEffect?
What has useEffect too. Without a dependency array it auto-tracks, like effect(fn): it runs after the component mounts and re-runs when a signal it reads changes, and the cleanup it returns runs before the next run and on unmount. With a dependency array it re-runs only for the accessors listed: [count], never [count()], which is a plain value and reports ERR_EFFECT_DEP_NOT_FUNCTION. [] runs it once, after mount. Either way, you'll never have a stale closure or missing dependency bug again.
import { useSignal, useEffect } from 'what-framework';
function Title() {
const count = useSignal(0);
const step = useSignal(1);
// Re-runs when count changes, and not when step changes
useEffect(() => {
document.title = `Count: ${count()} (step ${step()})`;
}, [count]);
return <button onClick={() => count.set((c) => c + step())}>{count()}</button>;
}
Opting out of tracking
Sometimes you want to read a signal without subscribing to it. Use peek() or untrack():
effect(() => {
// Re-runs when count changes, but NOT when multiplier changes
const result = count() * multiplier.peek();
console.log(result);
});
// Or with untrack()
effect(() => {
const result = count() * untrack(() => multiplier());
console.log(result);
});
peek() is shorthand for reading one signal. untrack() wraps a block of code where nothing inside creates subscriptions.
Lifecycle: onMount vs useEffect(fn, [])
In React, "run once on mount" is useEffect(() => {}, []). That works in What too, and runs once after mount, but the idiomatic form is onMount:
| React | What |
|---|---|
useEffect(fn, []) |
onMount(fn) |
useEffect(() => { return cleanup }, []) |
onMount(fn) plus onCleanup(cleanup) |
useEffect(fn, [dep]) |
effect(fn) (auto-tracks), or useEffect(fn, [dep]) with dep the accessor |
| Cleanup on unmount | onCleanup(fn) |
One difference worth memorising: unlike React's useEffect, onMount ignores a function you return from it. The return value is dropped, so a React teardown ported one-to-one leaks its listener or timer silently. Register teardown with onCleanup instead, as below.
import { onMount, onCleanup } from 'what-framework';
function Chat({ roomId }) {
let socket;
onMount(() => {
socket = new WebSocket(`wss://chat.example.com/${roomId}`);
});
onCleanup(() => {
socket?.close();
});
return <div>Connected to {roomId}</div>;
}
signal() vs useSignal()
signal() is the canonical API. Use it everywhere: module scope, inside components, and in stores. Because What components run once, a signal() declared in a component body executes exactly once (not per-render like a React hook), so there is no hook-ordering rule and no need for a separate component-only primitive.
| API | Where to use | Notes |
|---|---|---|
signal(value) |
Anywhere: module scope, components, stores | Canonical. Read with count(), write with count.set(v) (or the shorthand count(v)) |
useSignal(value) |
Inside components (optional) | A thin compat shim for React muscle memory. Returns the same signal object as signal(); identical behavior in the run-once model |
// Module-level signal: shared between all components
const theme = signal('dark');
function Counter() {
// Component-level signal: runs once, local to this component
const count = signal(0);
return <button onClick={() => count(c => c + 1)}>{count()}</button>;
}
Rule of thumb: reach for signal() everywhere. useSignal() exists if you prefer the React use* naming inside components, and it returns the same thing.
Quick Reference
| React | What | Notes |
|---|---|---|
useState |
useState or useSignal |
useState returns [accessor, setter]: read with count(), write with setCount(v). useSignal returns the signal: read with (), write with .set() |
useReducer |
useReducer |
Returns [accessor, dispatch]: read with state() |
useMemo |
useMemo or useComputed |
Returns an accessor (total()) that tracks what it reads; useMemo's dependency array is ignored |
useEffect(fn, [deps]) |
effect(fn) or useEffect(fn) |
Auto-tracks the signals it reads. useEffect(fn, [count]) re-runs only for the accessors listed |
useEffect(fn, []) |
onMount(fn) or useEffect(fn, []) |
Runs once after mount |
useRef |
useRef |
Same API |
useCallback |
Not needed | No re-renders means no stale closures. useCallback exists and returns your function unchanged |
React.memo |
Not needed | Components don't re-render by default |
createContext |
createContext |
Pass the signal to Provider, not its current value |
useContext |
useContext |
Call it in the component body, never in a handler |
Suspense |
Suspense |
Same pattern |
lazy() |
lazy() |
Same API; a failed download is tried again on the next render, where React keeps the error |
Context: two rules the run-once model adds
The names match React, but the run-once model changes how you feed and read a context. A Provider body also runs once, so a value computed from a signal is captured at that moment and never refreshed. Pass the signal itself and let the consumer call it. And read the context during the component body: useContext called inside an event handler has no component to walk up from, so it returns the context's default value, and development reports ERR_HOOK_OUTSIDE_COMPONENT.
// BROKEN: value is read once, consumers never see a change
<Theme.Provider value={theme()}><Panel /></Theme.Provider>;
// CORRECT: hand over the signal itself
<Theme.Provider value={theme}><Panel /></Theme.Provider>;
function Panel() {
const theme = useContext(Theme); // read in the body
const onClick = () => console.log(theme()); // close over it
return <div onClick={onClick}>{() => theme()}</div>;
}
Common Mistakes from React Habits
1. Using the signal as a value
On its own as a JSX child or attribute value, {count} is a live binding and needs no call. Anywhere else, the accessor is the function, not its value: in a template literal, in arithmetic or in a comparison it is converted to the function's source text, and development reports ERR_MISSING_SIGNAL_READ.
// React habit: count is already a value
const count = useSignal(0);
<p>{`Count: ${count}`}</p>
// What: count is a function, call it
<p>{`Count: ${count()}`}</p>
2. Inline math without useComputed
// React habit: works because the component re-runs
const doubled = count() * 2; // computed once, never updates
// What: need useComputed (or useMemo) for derived values
const doubled = useComputed(() => count() * 2);
3. Writing a signal in the component body
// Harmless but pointless. The body runs once, so this is a slow useSignal(1)
function Bad() {
const x = useSignal(0);
x.set(1);
return <p>{x()}</p>;
}
// Start the signal at the value you want
function Good() {
const x = useSignal(1);
return <p>{x()}</p>;
}
4. Listing values in a dependency array
effect() takes no dependency array: its second argument is options, so an array there is ignored and the effect tracks what it reads. useEffect takes one, and it lists accessors. [count()] passes the number read once at setup, which never changes, so the effect never re-runs for it, and development reports ERR_EFFECT_DEP_NOT_FUNCTION.
// React habit: list the values the effect reads
const count = useSignal(0);
useEffect(() => {
document.title = `Count: ${count()}`;
}, [count()]);
// What: leave the array out, and the effect tracks what it reads
useEffect(() => {
document.title = `Count: ${count()}`;
});
// Or list the accessor itself
useEffect(() => {
document.title = `Count: ${count()}`;
}, [count]);
Using React Libraries in What
You don't have to choose between What and the React ecosystem. The what-react compat layer runs real React libraries on What's engine. The libraries verified end to end (headless browser plus CI) are zustand, TanStack Query, react-hook-form, react-hot-toast, Headless UI, Framer Motion and Recharts. Anything beyond those seven is plausible but untested on the current runtime, so treat it as unverified until you have tried it.
Inside a compat component, React semantics apply, not What's
Compat works by aliasing every react and react-dom import to what-react and pointing JSX at the React runtime, for the whole project. Inside a compat component the rules above do not apply: hooks return values rather than accessors, and a bare {count()} renders once and never updates. Compat components are also client-only and cannot be server-rendered.
Compat roots and portal containers delegate React events in capture and bubble order. Events inside a portal follow their React ancestors. A checkbox or radio reports onChange during the click, and a text field reports edits during input.
// vite.config.js: reactCompat() sets up the aliases and the JSX runtime
import { defineConfig } from 'vite';
import { reactCompat } from 'what-react/vite';
export default defineConfig({
plugins: [reactCompat()],
});
With that in place, the component is ordinary React code and the library works untouched:
import { mount } from 'what-framework';
import { create } from 'zustand';
const useStore = create((set) => ({
count: 0,
increment: () => set((s) => ({ count: s.count + 1 })),
}));
function App() {
const count = useStore((s) => s.count);
const increment = useStore((s) => s.increment);
// React semantics: count is a number, not an accessor
return <button onClick={increment}>{count}</button>;
}
mount(<App />, '#app');
The fastest way to get a working project is create-what with React support enabled, which wires the plugin and ships this zustand demo. See the React Compat docs for the full verified matrix and the known limitations: no server rendering, a minimal Suspense that unmounts suspended subtrees (losing their state), and errors thrown inside effects being logged rather than routed to an error boundary.