Skip to content

Actions as data

In most React apps, the answer to “what happens when this button is clicked?” is a callback closed over the surrounding component. The behaviour is invisible until you run it. input-actions flips this: the answer is a tuple — a value you can log, inspect, or send over the wire.

['action/save', ['user', 'name'], 'Alex']

That’s the entire description of a state change. The first element names the action. The second is a path into state. The third is the new value. A small executor interprets it.

Click the buttons below. Every tuple you dispatch shows up underneath — same data the library’s executor sees.

0

Last dispatched actions

  1. Click a button — the action tuple you dispatch shows up here.

This component is a few lines:

import { useLocalActions } from '@input-actions/core';
function Counter() {
const { state, actions } = useLocalActions({ count: 0 });
return (
<>
<button onClick={actions(['action/decrement', ['count']])}>−</button>
<span>{state.count}</span>
<button onClick={actions(['action/increment', ['count']])}>+</button>
</>
);
}

actions(...) takes an action tuple and returns an event handler. The data inside the tuple is what the executor actually cares about — the function wrapper is just the bridge to React’s event API.

Encoding state changes as data unlocks several capabilities for free:

  • Inspectability. The demo above is the simplest version: a list of recent dispatches. The same idea scales to time-travel devtools, action replay in tests, and bug reports that include the literal action history.
  • Composition. ['action/batch', [['action/save', ...], ['action/save', ...]]] is just data. You can build pipelines, conditional sequences, or rerunable scripts.
  • Late binding. Event-derived values use placeholders: 'event/target.value', 'event/target.checked', 'event/timestamp'. The executor resolves them at dispatch time, so the action tuple isn’t capturing live DOM references — it’s still pure data.
  • Cross-process. Actions are JSON-serialisable. Sending one across an iframe, a worker, a websocket, or a tab is just JSON.stringify.

The full set is enumerated in Action from @input-actions/core. The most common are:

ActionEffect
['action/save', path, value]Set the value at path
['action/toggle', path]Flip a boolean at path
['action/increment', path, by?]Add by (default 1) to a number at path
['action/decrement', path, by?]Subtract by (default 1) from a number at path
['action/push', path, item]Push to an array at path
['action/merge', path, object]Shallow-merge an object into the value at path
['action/reset', path?]Reset everything (or one slice) to the initial state
['action/batch', [actions...]]Dispatch the nested actions in order

The async ('action/async-*') and machine ('machine/*') variants have their own concepts pages (coming soon).

You don’t write a switch statement. The executor knows how to interpret every action type the library ships with, plus the async + machine families. To add a new behaviour, you usually compose existing actions with 'action/batch'. To register custom side effects, you supply handlers (asyncHandlers) keyed by name — also data, not callbacks.