elgerlambert / redux-localstorage

Store enhancer that syncs (a subset) of your Redux store state to localstorage.
MIT License
1.32k stars 107 forks source link

1.0.0-rc4 doesn't work on react-native due to transpilation issue #44

Closed seansfkelley closed 8 years ago

seansfkelley commented 8 years ago

This issue was erroneous; I was comparing the head of the 1.0 branch with a release that was built from a different, earlier commit. The original issue follows.


persistState.js has the following snippet:

function getDefaultStorage() {
  return adapter(localStorage);
}

// snip

export default function persistState(storage = getDefaultStorage(), key = defaultKey, callback) {

I figure it's written this way so that if you provide a non-default storage mechanism (i.e. AsyncStorage, in this case), it never creates the default adapter and therefore never hits the localStorage reference. Unfortunately, it seems like Babel has decided that this function is inlinable, so the transpiled code ends up like so:

var defaultStorage = (0, _adaptersLocalStorage2['default'])(localStorage);

// snip

function persistState(storage, key, callback) {
  if (storage === undefined) storage = defaultStorage;

So it always hits the localStorage reference, throwing an exception at runtime on React Native:

localStorage is not defined
seansfkelley commented 8 years ago

While doing some more poking around I realized this has already been fixed by #26, just not released. Closing in favor of #45.