Skip to content

Migration overview & common pitfalls

This section helps you transition from common React state-management patterns to input-actions. It’s split by starting point — pick the guide that matches what you’re using today.

If you’re starting a new project, skip ahead to the Quickstart. This section is for code that already exists.

A quick tour of patterns that input-actions is designed to remove. Each is a small, real situation; the fix column is the canonical input-actions shape.

// Plain React — broken: increments stop being correct on rapid clicks
const [count, setCount] = useState(0);
<button onClick={() => setTimeout(() => setCount(count + 1), 500)}>
+1 in 500ms
</button>

Click the button three times quickly: the count goes from 0 → 1 instead of 0 → 3. Each closure captures the count value at click time; later callbacks operate on stale data.

// input-actions: action data describes the operation, not the value
const { state, actions } = useLocalActions({ count: 0 });
<button
onClick={() =>
setTimeout(() => actions(['action/increment', ['count']])(), 500)
}
>
+1 in 500ms
</button>

['action/increment', ['count']] says “add 1 to whatever count is at dispatch time.” No value capture. Three clicks become three increments.

// Plain React — onChange passed through three levels just to reach the field
function App() {
const [user, setUser] = useState({ name: '', email: '' });
return <Page user={user} onUserChange={setUser} />;
}
function Page({ user, onUserChange }) { return <Form user={user} onUserChange={onUserChange} />; }
function Form({ user, onUserChange }) {
return (
<input
value={user.name}
onChange={(e) =>
onUserChange({ ...user, name: e.target.value })
}
/>
);
}
// input-actions: one provider at the top, useStore + useActions where the data is read or written
const store = createSimpleStore({ user: { name: '', email: '' } });
function App() {
return (
<StoreProvider store={store}>
<Page />
</StoreProvider>
);
}
function Form() {
const name = useStore((s) => s.user.name);
const actions = useActions();
return (
<input
value={name}
onChange={actions(['action/save', ['user', 'name'], 'event/target.value'])}
/>
);
}

No prop drilling. Page doesn’t have to know Form exists or what it edits.

// Plain React — easy to forget; submission accidentally reloads the page
<form onSubmit={() => dispatch(submitForm())}>
// input-actions: preventDefault is part of the action list, not the wrapper
<form onSubmit={actions([
['action/event', 'event/preventDefault'],
['action/async-prevent', 'submit', 'submitForm'],
])}>

The action’s intent and its side effect on the DOM event are both data. Nothing to forget.

4. useEffect chains coordinating async state

Section titled “4. useEffect chains coordinating async state”
// Plain React — three booleans, two effects to keep them coherent
const [isLoading, setIsLoading] = useState(false);
const [hasError, setHasError] = useState(false);
const [data, setData] = useState(null);
useEffect(() => {
if (!isLoading) return;
fetchUser().then(
(d) => { setData(d); setHasError(false); setIsLoading(false); },
() => { setHasError(true); setIsLoading(false); },
);
}, [isLoading]);
useEffect(() => {
if (data) trackAnalytics(data);
}, [data]);

Four pieces of state, two effects, easy to make incoherent.

// input-actions: one async action; lifecycle is in `_async`, follow-ups are data
dispatch(['action/async', 'fetch-user', 'fetchUser', {
strategy: 'prevent',
onSuccess: [['action/push', ['analytics'], 'user-loaded']],
}]);
// Read the lifecycle, no effects:
const op = useAsyncOperation('fetch-user');
// op.isLoading, op.hasError, op.data

No state synchronisation. The library normalises the async lifecycle.

// Plain React — every dispatch in a parent re-renders all children
function Parent() {
const [user, setUser] = useState({ name: '', email: '' });
const [count, setCount] = useState(0);
// every setCount call re-renders Children that only depend on user
return <>
<Counter count={count} onIncrement={() => setCount(count + 1)} />
<UserDisplay user={user} />
</>;
}
// input-actions: per-component selectors via useStore
function Counter() {
const count = useStore((s) => s.count); // re-renders ONLY when count changes
const actions = useActions();
return (
<button onClick={actions(['action/increment', ['count']])}>
{count}
</button>
);
}
function UserDisplay() {
const user = useStore((s) => s.user); // re-renders ONLY when user changes
return <span>{user.name}</span>;
}

Sibling components don’t re-render each other. The selector defines the subscription.

6. Boolean state explosion for state machines

Section titled “6. Boolean state explosion for state machines”
// Plain React — combinatorial booleans encode states implicitly
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
const [hasData, setHasData] = useState(false);
const [isStale, setIsStale] = useState(false);
// "Is the data definitely loaded and not refreshing?" → !isLoading && hasData && !isStale && !isError

Four booleans = 16 combinations, most nonsensical. Bugs lurk in the unrepresented cases.

// input-actions: an explicit machine with named modes
const dataMachine = {
name: 'data',
initialState: 'idle',
states: {
idle: { allowedTransitions: ['loading'] },
loading: { allowedTransitions: ['ready', 'error'] },
ready: { allowedTransitions: ['refreshing'] },
refreshing: { allowedTransitions: ['ready', 'error'] },
error: { allowedTransitions: ['loading'] },
},
};

Five modes. The validator prevents impossible transitions. UI conditions read state.data.mode === 'ready', not a boolean salad.

// Plain React — what happened that led to this bug? Good luck.
const handleClick = () => {
const next = computeSomething(state);
setSomething(next);
};

The behaviour is a closure — invisible until it runs. To repro a bug you need the user’s full session.

// input-actions: every state change is a tuple. Log it, replay it, share it.
const handleClick = actions(['action/save', ['something'], 'computed-value']);

Action tuples are JSON-serialisable data. Add a logger, export a session, or — when paired with the Redux adapter — get time-travel debugging in DevTools for free.

Across all the pitfalls above, the same shift is happening:

Plain Reactinput-actions
Behaviour lives in closuresBehaviour lives in tuples
State synchronisation via effectsState derivation via selectors
Component-private state machines (booleans)Explicit FSMs with validated transitions
Bug repro requires a videoBug repro is a list of tuples

ADR 0020 records the philosophy: every state change is data; components are visual building blocks; UI props are pure derivations from state.

Already using Redux, Zustand, Jotai, MobX, or thi.ng?

Section titled “Already using Redux, Zustand, Jotai, MobX, or thi.ng?”

You don’t need to migrate the underlying store — install the matching adapter and start dispatching input-actions tuples through your existing setup. The adapter packages keep the native API available (Redux DevTools, Jotai atoms, MobX autorun, thi.ng undo/redo).