Skip to content

Commit

Permalink
Fix cookie not being parsed on load.
Browse files Browse the repository at this point in the history
If you have to stringify your object before saving, and you add that,
to the `_cookies` cache, then you'll return that on `load` after setting
it with `save`.

While if the value is already set, the script correctly parses the cookie
values and what you load is returned as an object.

This oneliner takes care of that for you.
  • Loading branch information
pixeleet committed Apr 19, 2015
1 parent 3b1780c commit 877e9a5
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ function load(name) {

function save(name, val, opt) {
_cookies[name] = val;

// Cookies only work in the browser
if (typeof document === 'undefined') return;

// allow you to work with cookies as objects.
if (typeof val === 'object') val = JSON.stringify(val);

document.cookie = cookie.serialize(name, val, opt);
}

Expand All @@ -34,4 +37,4 @@ if (typeof module !== 'undefined') {

if (typeof window !== 'undefined') {
window['reactCookie'] = reactCookie;
}
}

0 comments on commit 877e9a5

Please sign in to comment.