# Component hooks (https://docs.salla.dev/twilight/react/sdk/component-hooks)



Read this when you need to change how a `salla-*` web component behaves, from your theme or an app script.

**Component hooks** let your code step into a web component at fixed points: when it loads, before it adds to the cart, before it shows its products.
They are not [slots](https://docs.salla.dev/twilight/react/guide/hooks-and-slots): a slot adds content to a page, a component hook changes a component.

```ts
const unsubscribe = Salla.hooks.registerHook('salla-add-product-button', 'componentDidLoad', (button) => {
  console.log('Add button for product', button.productId);
});

// later, to stop:
unsubscribe();
```

## Register a hook [#register-a-hook]

`Salla.hooks.registerHook(tag, hookName, callback, options?)` returns a function that removes the hook.

| Option     | Does                                                                                       |
| ---------- | ------------------------------------------------------------------------------------------ |
| `priority` | A number. Higher runs first. `0` by default.                                               |
| `once`     | `true` to run once, then remove itself. `Salla.hooks.registerHookOnce(...)` does the same. |

The callback may be `async`: the component waits for it. An error in your callback is caught and logged, so it never breaks the component.

## Components and hooks [#components-and-hooks]

Only these tags and hooks can be registered. Anything else is ignored with a console warning.

| Tag                                                                      | Hook                                    | Callback receives        | You can change                                   |
| ------------------------------------------------------------------------ | --------------------------------------- | ------------------------ | ------------------------------------------------ |
| `salla-add-product-button`                                               | `componentDidLoad`                      | the component            | The element                                      |
| `salla-add-product-button`                                               | `validate`                              | `{ isValid, component }` | `isValid`: set it to `false` to stop the add     |
| `salla-products-list`                                                    | `componentDidLoad`                      | the component            | The element                                      |
| `salla-products-list`                                                    | `beforeBuildListUrl`                    | `{ component, url }`     | `url`, to change the request                     |
| `salla-products-list`                                                    | `beforeAppendItems`                     | `{ component, items }`   | `items`, to filter or replace the products shown |
| `salla-products-slider`                                                  | `componentDidLoad`, `beforeAppendItems` | as above                 | as above                                         |
| `salla-menu`                                                             | `afterFetchMenuItems`                   | the component            | `component.menus`, the loaded menu               |
| `salla-product-options`, `salla-hook`, `salla-scopes`, `salla-user-menu` | `componentDidLoad`                      | the component            | The element                                      |

On `salla-add-product-button`, the component has `productId`, `productType`, `productStatus` and `quantity`.

```ts
Salla.hooks.registerHook('salla-products-list', 'beforeAppendItems', (ctx: { items: Array<{ is_available: boolean }> }) => {
  ctx.items = ctx.items.filter((item) => item.is_available);
});

Salla.hooks.registerHook('salla-products-list', 'beforeBuildListUrl', (ctx: { url: string }) => {
  const url = new URL(ctx.url);
  url.searchParams.set('custom_filter', 'my_value');
  ctx.url = url.toString();
});
```

## Late registration [#late-registration]

You do not have to register before the component loads. A new `componentDidLoad` hook also runs at once for every component of that tag already on the page,
then for each one that loads later. Each element runs it only once.

## Remove hooks [#remove-hooks]

| Call                                              | Removes                                          |
| ------------------------------------------------- | ------------------------------------------------ |
| The function `registerHook` returned              | That one hook                                    |
| `Salla.hooks.removeHook(tag, hookName, callback)` | That one hook                                    |
| `Salla.hooks.removeHook(tag, hookName)`           | Every hook of that name on the tag               |
| `Salla.hooks.clear(tag?)`                         | Every hook on the tag, or every hook on the page |

In a React component, register in `useEffect` and return the remove function as the cleanup.

## Gotchas [#gotchas]

* `validate` runs only when the button is not a form's submit button and the product's `salla-product-options` is on the page.
  The engine's product page submits a form, so there `validate` does not run. To check an add there, use your own submit handler.
* Register after the SDK has started: inside `Salla.onReady(() => { ... })`, or in `useEffect`.
* Objects are passed by reference. Change the fields of `ctx`; do not replace `ctx` itself.

## See also [#see-also]

* [Hooks and slots](https://docs.salla.dev/twilight/react/guide/hooks-and-slots)
* [Web components](https://docs.salla.dev/twilight/react/web-components)
* [Events](https://docs.salla.dev/twilight/react/sdk/events)
