> For the complete documentation index, see [llms.txt](https://reimjs.gitbook.io/reim/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://reimjs.gitbook.io/reim/guide/subscribe.md).

# Subscribe

## What is subscribe?

with `subscribe` function you will be notified whenever there is a change by simply

```javascript
const unsubscribe = store.subscribe(state => {
    console.log('state updated!')
})
```

And when you want to stop it just use the returned function

```javascript
unsubscribe()
```

## Filter

Sometimes you want to only be notified if some of the properties changed, you can use the `getter` option, which returns a derived state from store.

```javascript
const countStore = reim({
    count: 2,
    message: ''
})

countStore.subscribe(state => {
    console.log('state count changed!')
    console.log(state.count) // 12
    console.log(state.messsage) // undefined
}, {
    filter: state => ({count: state.count})
})

// Will not trigger subscriber
countStore.setState({message: 'should not trigger'})

// 'state count changed'
countStore.setState(state => {
    state.count += 10
})
```
