# wishlist (https://docs.salla.dev/twilight/react/sdk/wishlist)



Use `Salla.wishlist` to change the customer's wishlist. In components, the [`useWishlist`](https://docs.salla.dev/twilight/react/hooks/use-wishlist) hook wraps it and keeps your hearts in sync, so reach for that first.

```tsx title="app/components/product/WishlistButton.tsx"
export function WishlistButton({ productId }: { productId: number }) {
  return (
    <button onClick={() => window.salla?.wishlist.toggle(productId)}>
      Save
    </button>
  );
}
```

## Methods [#methods]

| Method           | Parameters               | Does                                                                         |
| ---------------- | ------------------------ | ---------------------------------------------------------------------------- |
| `add`            | `id` or `{ id }`         | Saves the product                                                            |
| `remove`         | `id` or `{ id }`         | Removes the product                                                          |
| `toggle`         | `id`                     | Removes the product when it is saved, saves it otherwise                     |
| `has`            | `id`                     | `true` when the product is saved. No request.                                |
| `getWishlistIds` | none                     | The ids of the saved products. No request.                                   |
| `share`          | `productIds, selectAll?` | Creates a share link for those products, or for all of them with `selectAll` |

`has` and `getWishlistIds` read the ids saved in the browser. For a logged-in customer the SDK fills them from the server when the page starts,
then fires `wishlist::storage.synced`.

## Guests [#guests]

The wishlist needs a logged-in customer. For a guest, `add`, `remove` and `toggle` open the login popup and reject with a "must log in" message.
After the customer logs in, the SDK finishes the add or remove by itself, even when the login reloaded the page.

## Stop or change an action [#stop-or-change-an-action]

Before it sends a request, the SDK fires a `before` event with an object you can change. Set `preventDefault` to `true` to cancel:

```ts
Salla.event.on('wishlist::before.add.item', (data: { product_id: number; preventDefault: boolean }) => {
  if (data.product_id === 1234) data.preventDefault = true; // this product cannot be saved
});
```

`wishlist::before.share` passes `{ product_ids, select_all, preventDefault }`, and the SDK uses your changed ids.

## Events [#events]

| Event                                                        | Fires when                              | Arguments                                     |
| ------------------------------------------------------------ | --------------------------------------- | --------------------------------------------- |
| `wishlist::before.add.item` / `wishlist::before.remove.item` | Before `add` / `remove`                 | `{ product_id, preventDefault }`              |
| `wishlist::added` / `wishlist::addition.failed`              | `add`                                   | response or error, product id                 |
| `wishlist::removed` / `wishlist::removing.failed`            | `remove`                                | response or error, product id                 |
| `wishlist::before.share`                                     | Before `share`                          | `{ product_ids, select_all, preventDefault }` |
| `wishlist::shared` / `wishlist::sharing.failed`              | `share`                                 | response or error, product ids                |
| `wishlist::storage.synced`                                   | The saved ids were read from the server | none                                          |

## Gotchas [#gotchas]

* `toggle` decides from the ids saved in the browser. It has no events of its own: it fires those of `add` or `remove`.
* A `before` handler that throws does not stop the action. Use `preventDefault`.

## See also [#see-also]

* [`useWishlist`](https://docs.salla.dev/twilight/react/hooks/use-wishlist)
* [Wishlist route](https://docs.salla.dev/twilight/react/routes/account-wishlist)
* [`wishlist` API module](https://docs.salla.dev/twilight/react/api/modules/wishlist)
