---
description: Sets (replaces whole existing) extra in an item.
---

# setExtra(key, extra)

{% hint style="info" %}
As existing extra is being entirely overwritten, be careful how / when you use it. Simply, use this method with caution.
{% endhint %}

### **Arguments**

1. `key` _(string)_: Key to store the extra under (represents given item).
2. `extra` _(object)_: Extra data to replace existing one.

### **Throws**

1. If `extra` is not an object, it will throw `'extra' must be an object.`

### **Lifecycle**

1.  **preSetExtra**

    **Event name:** `preSetExtra`\
    **Properties passed:**

    * `cacheInstance` reference to cache instance (`this`)
    * `key` key passed to `setExtra` method
    * `extra` extra passed to `setExtra` method

    **Returns:** _(object)_: object containing properties:

    * `cacheInstance` reference to cache instance (`this`)
    * `key` key passed through handlers added for `preSetExtra` event
    * `extra` extra passed through handlers added for `preSetExtra` event\

2.  **postSetExtra**

    **Event name:** `postSetExtra`\
    **Properties passed:**

    * `cacheInstance` reference to cache instance (`this`) returned by `preSetExtra`
    * `key` key returned by `preSetExtra`
    * `extra` extra (object) returned by adapter using its [setExtra](../adapter/setextra-key-extra) method

    **Returns:** _(object)_: object containing properties:

    * `cacheInstance` reference to cache instance (`this`)
    * `key` key passed through handlers added for `postSetExtra` event
    * `extra` extra passed through handlers added for `postSetExtra` event\


    Eventually `setExtra` returns extra returned by `postSetExtra`.

### **Example**

```javascript
// Existing extra
{ 
    some: 'data'
}

cache.setExtra(key, { foo: 'bar' });

// Extra after setting new one
{
    foo: 'bar'
}
```
