react-localstorage
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -63,3 +63,11 @@ 'use strict'; | ||
// create a custom Chrome launcher without the support for localStorage | ||
customLaunchers: { | ||
Chrome_without_ls: { | ||
base: 'Chrome', | ||
flags: ['--disable-local-storage'] | ||
} | ||
}, | ||
// Continuous Integration mode | ||
@@ -66,0 +74,0 @@ // if true, Karma captures browsers, runs the tests and exits |
{ | ||
"name": "react-localstorage", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "A mixin for automatically synchronizing a component's state with localStorage.", | ||
@@ -5,0 +5,0 @@ "main": "react-localstorage.js", |
'use strict'; | ||
var React = require('react'); | ||
var invariant = require('react/lib/invariant'); | ||
var ls = global.localStorage; | ||
var warn = require('react/lib/warning'); | ||
var hasLocalStorage = 'localStorage' in global; | ||
var ls, testKey; | ||
if (hasLocalStorage) { | ||
testKey = 'react-localstorage.mixin.test-key'; | ||
try { | ||
// Access to global `localStorage` property must be guarded as it | ||
// fails under iOS private session mode. | ||
ls = global.localStorage; | ||
ls.setItem(testKey, 'foo'); | ||
ls.removeItem(testKey); | ||
} catch (e) { | ||
hasLocalStorage = false; | ||
} | ||
} | ||
// Warn if localStorage cannot be found or accessed. | ||
warn( | ||
hasLocalStorage, | ||
'localStorage not found. Component state will not be stored to localStorage.' | ||
); | ||
var Mixin = module.exports = { | ||
@@ -18,3 +39,3 @@ /** | ||
componentDidUpdate: function(prevProps, prevState) { | ||
if (!ls || !this.__stateLoadedFromLS) return; | ||
if (!hasLocalStorage || !this.__stateLoadedFromLS) return; | ||
var key = getLocalStorageKey(this); | ||
@@ -42,2 +63,3 @@ var prevStoredState = ls.getItem(key); | ||
componentDidMount: function () { | ||
if (!hasLocalStorage) return; | ||
var me = this; | ||
@@ -76,3 +98,3 @@ loadStateFromLocalStorage(this, function() { | ||
function getDisplayName(component) { | ||
// at least, we cannot get displayname | ||
// at least, we cannot get displayname | ||
// via this.displayname in react 0.12 | ||
@@ -79,0 +101,0 @@ return component.displayName || component.constructor.displayName; |
13373
308