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



Read this when browser code must remember something between pages, or read what the SDK remembers about the cart and the customer.

`Salla.storage` saves values in the browser's local storage, so they survive a reload and a new tab.
If local storage is blocked, it falls back to session storage, then cookies, then memory.

## Save, read and remove [#save-read-and-remove]

```ts
Salla.storage.set('my-theme.last-category', 42);
Salla.storage.get('my-theme.last-category');          // 42
Salla.storage.get('my-theme.missing', 'default');     // 'default'
Salla.storage.remove('my-theme');
```

Dots reach into a nested object: `set('my-theme.last-category', 42)` saves `{ "last-category": 42 }` under the key `my-theme`.
Values can be any JSON value.

## Expire a value [#expire-a-value]

`setWithTTL` saves a value for a number of minutes (10 by default). `getWithTTL` returns the default once it has expired.

```ts
Salla.storage.setWithTTL('promo-closed', true, 60);   // one hour
Salla.storage.getWithTTL('promo-closed', false);      // true, then false after an hour
```

The last argument picks where to save: `'store'` (local storage, the default), `'session'` or `'cookie'`.
These keys are saved per store, so the same browser on two stores does not mix them up.

## What the SDK saves [#what-the-sdk-saves]

The SDK keeps these keys itself. Read them, but change them only through the SDK methods.

| Key                   | Holds                                                                                                               |
| --------------------- | ------------------------------------------------------------------------------------------------------------------- |
| `cart.id`             | The current cart's id                                                                                               |
| `cart.summary`        | `count`, `total`, `sub_total`, `discount`, `tax_amount`, `total_discount`, `shipping_cost` and `real_shipping_cost` |
| `token`               | The logged-in customer's token                                                                                      |
| `user`                | The customer's profile, saved at login                                                                              |
| `salla::wishlist`     | The ids of the products in the wishlist                                                                             |
| `salla::availability` | The ids of products the customer asked to be told about when back in stock                                          |

For the cart count in a component, listen to `cart::updated` instead of reading storage: storage does not tell you when it changes. See [Events](https://docs.salla.dev/twilight/react/sdk/events).

## Clear [#clear]

`Salla.storage.clearAll()` removes only the keys in the table above, as at logout. Your own keys stay.
`Salla.storage.clearAll(true)` removes everything in local storage.

## Cookies [#cookies]

`Salla.cookie` works the same way for cookies:

| Method                                 | Does                                               |
| -------------------------------------- | -------------------------------------------------- |
| `Salla.cookie.get(name)`               | Reads a cookie                                     |
| `Salla.cookie.set(name, value, days?)` | Saves a cookie, for 10 days unless you pass `days` |
| `Salla.cookie.remove(name)`            | Deletes a cookie                                   |

## Gotchas [#gotchas]

* Storage exists only in the browser. Read it in `useEffect` or an event handler, never while rendering.
* Give your keys your own prefix, such as your theme name, so they never clash with the SDK's.
* Do not write `cart` or `token` yourself. The SDK resets the cart when its saved data does not match the store or the customer.

## See also [#see-also]

* [Config](https://docs.salla.dev/twilight/react/sdk/config)
* [Cart](https://docs.salla.dev/twilight/react/sdk/cart)
* [Overview](https://docs.salla.dev/twilight/react/sdk)
