All core hooks are imported fromDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/sanchedev/fraxel/llms.txt
Use this file to discover all available pages before exploring further.
fraxel/hooks and called inside component functions. They form the foundation of Fraxel’s reactivity and lifecycle system — useSignal and useComputed provide reactive state, useEffect and useMount manage lifecycle, and the remaining hooks wire together nodes, events, game controls, and cross-component communication. Every hook must be called at the top level of a component function, never inside conditionals or loops.
Quick Reference
| Hook | Signature | Description |
|---|---|---|
useSignal | useSignal<T>(initialValue: T): [SignalGetter<T>, SignalSetter<T>] | Creates a reactive signal with an initial value |
useComputed | useComputed<T>(fn: () => T): SignalGetter<T> | Creates a derived signal that recomputes when dependencies change |
useEffect | useEffect(fn: () => void | (() => void)): void | Runs an effect on mount and whenever tracked signals change |
useNode | useNode<T extends PrimaryNode>(type: T): NodeReference<T> | Creates a typed reference to pass as a JSX ref prop |
useEvent | useEvent<N, K>(node: NodeReference<N>, event: K, listener): void | Subscribes to a node event with automatic cleanup on destroy |
useMount | useMount(fn: () => void | (() => void)): void | Runs once on mount; returned function is called on destroy |
useSpawn | useSpawn<T>(node: NodeReference<T>): (jsx: Fraxel.Node) => void | Returns a function to dynamically add children to a node |
useGame | useGame(): GameControls | Returns game-level controls for play, pause, and scene management |
createContext | createContext<T>(defaultValue: T): ContextCreated<T> | Creates a context with a default value and a Provider component |
useContext | useContext<T>(context: ContextCreated<T>): T | Retrieves the current value of a context |
createTrigger | createTrigger<T extends any[]>(): Trigger<T> | Creates a Trigger instance for pub/sub communication |
useTrigger | useTrigger<T>(trigger: Trigger<T>, fn: Fun<T>): void | Subscribes to a Trigger with auto-cleanup on destroy |
useRef | useRef<T>(value: T): Reference<T> | Creates a mutable reference that persists across renders |
useChild | useChild<T>(path: (string | symbol)[], type: T): NodeReference<T> | Retrieves a child node reference by path |
useScript | useScript<T extends FraxelScript<PrimaryNode>>(node: NodeReference<PrimaryNode>): SignalGetter<T | undefined> | Retrieves the script instance attached to a node |
useSignal
useEffect or useComputed automatically registers it as a dependency — the effect or computed will re-run whenever the signal changes. When the owning node is destroyed, all subscribers are cleaned up automatically via clearSubs.
The initial value for the signal.
A callable function that returns the current signal value. Calling
getter() tracks it as a reactive dependency. Call getter.value() to read the value without tracking.A function that updates the signal value and notifies all subscribers. Equivalent to setting
signal.value = newValue.useComputed
fn is called immediately and re-evaluated automatically whenever any signal accessed inside it changes. Like useSignal, the computed signal cleans up its own subscriptions when the node is destroyed.
The computation function. Any
SignalGetter called inside this function is automatically tracked as a dependency.A callable getter that returns the current computed value and can itself be tracked as a dependency by other computed values or effects.
useEffect
fn when the node starts and again whenever any reactive signal accessed inside fn changes. Re-executions are batched — if multiple signals change synchronously in the same block, the effect runs only once before the next frame, deferred via queueMicrotask. If fn returns a cleanup function, that function is called when the node is destroyed.
The effect function. Any
SignalGetter called inside it is automatically tracked. May return a cleanup function that runs when the node is destroyed.useEffect differs from useMount in that it re-runs when tracked signals change. Use useMount when you only need to run once on mount with no reactive dependencies.useNode
NodeReference for the given node type. Pass the returned reference to the ref prop on the corresponding JSX element. The reference is populated when the node mounts and throws a NodeNotInitializedError if accessed before mounting.
The node type to reference. Must be a member of the
PrimaryNode enum.A reference object with a
node property (the live node instance) and a signal getter (SignalGetter<NodeInstances[T] | null>) that tracks when the node becomes available.PrimaryNode enum values:
| Value | JSX tag | Description |
|---|---|---|
PrimaryNode.Sprite | <sprite> | Displays a texture |
PrimaryNode.Transform | <transform> | Positions and organizes children |
PrimaryNode.Group | <group> | Groups children without position management |
PrimaryNode.Collider | <collider> | Detects collisions |
PrimaryNode.RayCast | <ray-cast> | Projects a ray to detect colliders |
PrimaryNode.Clickable | <clickable> | Detects pointer events |
PrimaryNode.Rectangle | <rectangle> | Renders a filled/stroked rectangle |
PrimaryNode.Timer | <timer> | Time-based utility node |
PrimaryNode.Text | <text> | Renders text on the canvas |
PrimaryNode.AudioPlayer | <audio-player> | Plays audio buffers |
PrimaryNode.Camera | <camera> | Controls the viewport |
PrimaryNode.RigidBody | <rigid-body> | Adds physics simulation |
PrimaryNode.AnimationPlayer | <animation-player> | Plays frame-based animations |
useChild, /api/nodes-2d, /api/nodes-utility
useEvent
NodeReference. The subscription is type-safe — TypeScript will only allow event names and listener signatures that are valid for the given node type. The listener is connected when the node starts and is automatically removed when the node is destroyed, preventing memory leaks.
The node reference to subscribe to.
The name of the event. TypeScript enforces valid event names for the node type.
The callback function. Its parameter types are automatically inferred from the event.
| Event | Node(s) | Payload |
|---|---|---|
started | All nodes | () |
destroyed | All nodes | () |
updated | All nodes | (delta: number) |
colliderEntered | Collider, RayCast | (other: Collider) |
colliderExited | Collider, RayCast | (other: Collider) |
clicked | Clickable | () |
mouseEntered | Clickable | () |
mouseExited | Clickable | () |
mouseOver | Clickable | (position: Vector2) |
timeout | Timer | () |
timeChanged | Timer | (time: number) |
animationEnded | AnimationPlayer | () |
animationChanged | AnimationPlayer | (name: string) |
useMount
fn exactly once when the node starts. Unlike useEffect, it does not track signal dependencies and will never re-run. If fn returns a cleanup function, that cleanup is called automatically when the node is destroyed.
The mount function. May return a cleanup function that runs when the node is destroyed.
Use
useMount for side effects that depend only on external resources (audio contexts, subscriptions, timers) and have no reactive signal dependencies. For effects that should re-run when signals change, use useEffect instead.useSpawn
spawn function from event handlers or effects — not during the component’s initial render.
The node reference whose underlying node will become the parent of spawned children.
A function that accepts JSX and appends the rendered nodes as children of the target node.
useNode, /guide/hooks
useGame
GameControls object with methods to control the game loop and manage scenes. Safe to call from any component — useGame reads from the global Game singleton.
Returns:
Starts or resumes the game loop.
Pauses the game loop.
Loads and switches to the named scene. The scene must be registered in the
<Game> component.Preloads a scene while the game is running. Returns a function to activate the preloaded scene when ready.
Returns the game canvas dimensions as a
Vector2.createContext / useContext
createContext creates a context object with a default value and a Provider component. Wrap a subtree with <Context.Provider value={...}> to supply a value to all descendants. useContext retrieves the nearest ancestor provider’s value, or the default if no provider is present.
createContext
The value returned by
useContext when no matching Provider is found in the ancestor chain.An object with a
Provider JSX component (props: { value: T, children }) and the stored defaultValue.useContext
The context object returned by
createContext.The value provided by the nearest ancestor
Provider, or defaultValue if none exists./guide/hooks
createTrigger / useTrigger
createTrigger creates a typed Trigger instance that acts as a pub/sub channel. useTrigger subscribes a component to that trigger with automatic cleanup when the node is destroyed. Call trigger.emit(...args) from anywhere to invoke all connected listeners.
createTrigger
Returns:A
Trigger instance with connect, disconnect, and emit methods.useTrigger
The
Trigger to subscribe to.The callback to invoke when the trigger emits. Automatically disconnected when the node is destroyed.
Trigger class
Invokes all connected listeners with the provided arguments.
Manually adds a listener. Prefer
useTrigger inside components for automatic cleanup.Removes a previously connected listener.
useRef
Reference object with a mutable current property that persists across re-renders. Unlike useSignal, mutating ref.current does not trigger any reactive updates — it is a plain mutable container. Use it to store imperative values like frame counters, accumulated deltas, or external resource handles.
The initial value for the reference.
An object with a single
current: T property. Mutations to current do not trigger effects or computed updates.useChild
id values from the component root. The path is resolved after the node starts (on onFirst of the started event). useChild requires the component to have exactly one root node.
An ordered array of
id values forming a path from the root node to the target child. Each entry corresponds to a child node’s id attribute.The expected type of the target node. Used for type safety on the returned reference.
A typed reference to the child node, populated after the component starts.
useChild throws a HookRequiresNodeRootError if the component has more than one root node. Wrap siblings in a single <transform> or <group> if needed.useScript
FraxelScript instance attached to a node. Returns a reactive SignalGetter — the value updates reactively when the node’s script property changes. The signal value is undefined until the node is mounted.
The node reference whose attached script you want to access.
A reactive getter returning the script instance, or
undefined if the node has no script or is not yet mounted.useNode, /guide/hooks, /guide/reactivity