---
description: >-
  Adds extra to item's existing extra. New properties are added, existing are
  overwritten.
---

# addExtra(key, extra)

### **Arguments**

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

### **Throws**

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

### **Lifecycle**

1.  **preAddExtra**

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

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

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

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

2.  **postAddExtra**

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

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

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

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


    Eventually `addExtra` returns extra returned by `postAddExtra`.

### **Example**

```javascript
// Existing extra in an item
{ 
    some: 'data'
}

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

// Extra after adding new one
{
    foo: 'bar',
    some: 'data'
}

cache.addExtra(key, { foo: 'baz' });

// Extra after another round of adding stuff to it
{
    foo: 'baz', // <-- notice that this value's changed
    some: 'data'
}
```
