Skip to content

Non-UI snippets

Validators and scripts

Not everything worth storing as editable code renders UI. Control panels accumulate validators, pricing rules, config builders: plain modules with no JSX.

compile vs compileModule#

tsx
compile(...) // insists on a component
compileModule(...) // returns whatever the snippet exported

useLiveModule#

useLiveModule: non-UI snippet

Example snippet (no default export required):

tsx
import { getCartCount, removeLastItem } from '@app/store';
export function run() {
removeLastItem();
return getCartCount();
}

Client usage:

tsx
const { exports, error, isCompiling } = useLiveModule({ code: source, modules });
const run = exports?.run;
run?.(); // call exported function

Typing exports#

Pass a type parameter. It is a claim, not a runtime check:

tsx
interface PricingRule {
validate: (n: number) => boolean;
total: (n: number) => number;
}
const { exports } = useLiveModule<PricingRule>({ code: source });