Hooks
Solar’s hooks manage state, async data, subscriptions, and lifecycle. All hooks must be called inside a component’srender function — never conditionally, never outside a component.
useState
Persists a value across renders. Returns the current value and a setter.The initial state value. Can also be a function — it runs once on the first render and its return value is used as the initial state (lazy initialization).
[value, setter]
The setter skips a re-render if the new value is strictly equal to the previous value.
useMemo
Caches an expensive computation. Recomputes only when deps change.The function to memoize. Runs on first render and whenever deps change.
Values that determine when to recompute. Can be an array or a plain object — compared by value, not reference.
useResource
Fetches async data. Re-fetches whenkey changes, and automatically cancels the in-flight request from the previous key.
Any serializable value. When it changes, the previous request is aborted and a new one starts. The previous data remains available during the transition (
data is not cleared to null).An async function that receives an
AbortSignal and returns the data. Pass the signal to fetch() or any other cancellable async operation.{ data, loading, error }
| Field | Type | Description |
|---|---|---|
data | any | The resolved value from the last successful fetch, or null on first load |
loading | boolean | True while a fetch is in flight |
error | Error | null | Set if the fetch threw, null otherwise |
useSubscription
Attaches an event listener and removes it when the component unmounts or whensource, event, or handler changes.
EventTargets (.addEventListener) and Node-style emitters (.on).
The object to attach the listener to.
The event name.
The event handler. Solar compares this by reference — if you need a stable handler, define it outside the render function or memoize it with
useMemo.onMount
Runs a function once after the component’s first render.Runs once. Not called on subsequent re-renders.
onUnmount
Registers a cleanup function that runs when the component is removed from the DOM.Runs once when the component is unmounted.