9.11. SolidJS Integration¶
Wire a ComponentVMOf<M> to a Solid component through createSignal and
onCleanup, Solid's fine-grained reactivity primitives.
9.11.1. Reactivity Primitive¶
createSignal(initial)returns a[get, set]pair. Reads are tracked inside JSX and reactive scopes; writes trigger re-renders for any subscriber.onCleanup(fn)registers teardown.
9.11.2. Mapping¶
| Solid | VMx |
|---|---|
createSignal(x, { equals: false }) |
PropertyChangedMessage<T> handler |
onClick={() => fn()} |
command.execute() in fn |
disabled on a button |
canExecuteChanged handler + canExecute() |
onCleanup(() => cleanup) |
dispose the subscription |
A republishModel() announcement keeps the same reference,
and a signal with the default === equality skips it. The hook creates the
signal with equals: false. Messages for another VM or another property are
filtered out.
9.11.3. Adapter Skeleton¶
This is the exact file run by examples/typescript/integration-recipes against
solid-js 1, the major version of the exact pin in the fixture's
package.json. The run covers an in-place mutation announced with
republishModel(), replacement and scalar updates, unrelated messages, the
Save button's enabled state, and cleanup.
import { createSignal, onCleanup, type Accessor } from "solid-js";
import {
type ComponentVMOf,
type ICommand,
type IMessageHub,
whenPropertyChanged,
} from "@thekaveh/vmx";
import type { Note } from "./note";
/**
* Binds one VM property for the owning component's lifetime. `vm` and `hub`
* are read once, so they are fixed inputs: remount (for example with a keyed
* `<Show>`) to rebind to another VM.
*/
export function useVm<M, K extends keyof ComponentVMOf<M>>(
vm: ComponentVMOf<M>,
hub: IMessageHub,
property: K,
): Accessor<ComponentVMOf<M>[K]> {
// `equals: false`: a republished model keeps its reference but must render.
const [value, setValue] = createSignal(vm[property], { equals: false });
const sub = whenPropertyChanged(hub, vm, property).subscribe(() => setValue(() => vm[property]));
onCleanup(() => sub.unsubscribe());
return value;
}
export function NoteView(props: {
vm: ComponentVMOf<Note>;
hub: IMessageHub;
saveCommand: ICommand;
}) {
const model = useVm(props.vm, props.hub, "model");
// Save is disabled while its command cannot run.
const [canSave, setCanSave] = createSignal(props.saveCommand.canExecute());
const canSaveSub = props.saveCommand.canExecuteChanged.subscribe(() =>
setCanSave(props.saveCommand.canExecute()),
);
onCleanup(() => canSaveSub.unsubscribe());
return (
<>
<h1>{model().title}</h1>
<button disabled={!canSave()} onClick={() => props.saveCommand.execute()}>
Save
</button>
</>
);
}
useVm reads vm and hub once, so they are fixed for the binding's
lifetime. To show a different VM, remount the component, for example inside a
keyed <Show>.
9.11.4. Fuller Example¶
There is no Solid Notes-Showcase. The runnable reference is
examples/typescript/integration-recipes. For a packaged adapter, see the
React recipe (react.md).