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

app/components/product/ShippingNote.tsx
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;
PropTypeDescription
namestringSlot name, such as "header:end". HookName enum values are valid. Required.
handler(context: T) => ReactNodeReceives the slot context and returns what to render, or null for nothing. Required.
priority?numberHigher 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 useHook is 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 name or priority changes.
  • For handlers that don't belong to a component, call hookRegistry.register or defineHooks from @salla.sa/twilight-theme-engine/hooks/HookRegistry at startup. This is how the starter theme does it, and it also works on the server.

Next

On this page