node package manager

mobx-cookie

logo

mobx-cookie

Synchronises a cookie's value with a MobX observable, allowing observers to react to its changes.

Install

yarn add mobx-cookie
# or 
npm install mobx-cookie # npm v5+ 

Example

import { action, autorun, extendObservable } from 'mobx'
import Cookie from 'mobx-cookie'
 
class Store {
  constructor() {
    extendObservable(this, {
      cookieName: new Cookie('name of cookie')
    })
 
    autorun('Log cookie', () => {
      console.log(this.cookieName.get())
    })
  }
 
  setCookie = action('Set cookie', value => {
    this.cookieName.set(value, { expires: 2 }) // 2 day expiry
  })
 
  removeCookie = action('Remove cookie', value => {
    this.cookieName.remove()
  })
}
 
const store = new Store()
 
// console: undefined
 
store.setCookie('testing')
 
// console: "testing"
 
store.removeCookie()
 
// console: undefined

Usage

Import package

import Cookie from 'mobx-cookie'

Instantiate Cookie class

Initialise and set the key name of the cookie

new Cookie(name[, options]) // see set() method for options

e.g.

@observable cookie = new Cookie('name of cookie in browser')
// or
constructor() {
  extendObservable(this, {
    cookie: new Cookie('name of cookie in browser')
  })
}

The observable now has the following methods:

get()

Retrieve the value of the cookie stored in this observable

this.cookie.get() // when cookie is updated, this will update too.

set(value[, options])

Set the value assigned to the observed cookie

this.cookie.set('value')

mobx-cookie is essentially a wrapper around the js-cookie package. Any options set as the second argument (as an object) will be passed to js-cookie.

e.g.

this.cookie.set('value', { expires: 2 }) // sets cookie to expire in two days.

remove()

Removes the cookie from the browser and sets the observable to undefined

this.cookie.remove()

License

MIT. See the LICENSE file for more information.

Credits

Thanks go to the creators of the two dependecies, js-cookie and moment.js, and of course to Michel Weststrate for MobX.

The logo is comprised of MobX's official logo's background by osenvosem and a cookie icon from icons8.