useHook
Register a component-scoped handler that renders content inside a named HookSlot.
Use useHook when a component should add content to a HookSlot somewhere else on the page, such as a shipping note under the add-to-cart form.
Example
import { useHook } from '@salla.sa/twilight-theme-engine/hooks/useHook';
export function ShippingNote() {
useHook('product:single.description.end', () => <p>Ships within 2 days.</p>);
return null;
}Render <ShippingNote /> once under your layout, and every slot with that name shows the note.
API
function useHook<T = Record<string, unknown>>(
name: string,
handler: HookHandler<T>,
priority?: number
): void;
type HookHandler<T> = (context: T) => ReactNode;| Prop | Type | Description |
|---|---|---|
name | string | Slot name, such as "header:end". HookName enum values are valid. Required. |
handler | (context: T) => ReactNode | Receives the slot context and returns what to render, or null for nothing. Required. |
priority? | number | Higher runs first. Default: 50. |
The context holds what the slot passed in, plus twilight (store, theme and more).
Gotchas
When the component unmounts, useHook removes only the handler it registered. Other handlers on the same slot, including the engine's own defaults, stay.
- The handler registers in an effect, so it runs in the browser only. Content added with
useHookis not in the server-rendered HTML. - The latest handler is always used, so it can read props and state without re-registering. Registration changes only when
nameorprioritychanges. - For handlers that don't belong to a component, call
hookRegistry.registerordefineHooksfrom@salla.sa/twilight-theme-engine/hooks/HookRegistryat startup. This is how the starter theme does it, and it also works on the server.
Next
HookSlotlists the slot names the engine renders.- Add content to a slot walks through registering at startup.
- Hooks and slots explains the two meanings of "hook".