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

Use window.localStorage to ensure the lib works in a mocha environment #33

Closed EvHaus closed 8 years ago

EvHaus commented 8 years ago

Simple fix to ensure the library works when being run in a mocha environment which doesn't use window as the global variable

jwhitley commented 8 years ago

The code around this has a deeper problem: localStorage may not be well defined at all, e.g. in a React Native environment. I just tried to give redux-localstorage a try in an RN project, but the code in persistState.js (1.0 branch) blows up with "localStorage is not defined", even though I was passing a valid storage to persistState built with adapter(AsyncStorage).

EvHaus commented 8 years ago

React Native doesn't support localStorage to begin with, so I don't think redux-localStorage will work for you there.

jwhitley commented 8 years ago

@globexdesigns, redux-localstorage isn't even a little bit specific to localStorage, speaking to the 1.0-breaking-changes branch:

Unopinionated store enhancer that persists state changes (locally).

It explicitly has adapters for several different stores including RN's AsyncStorage. localStorage is just one of the supported stores and happens to also be the default.

After my prior comment, I discovered that HEAD of 1.0-breaking-changes works correctly with AsyncStorage due to the fix in 61f9db95237a090daff2968ae1cae3b9252d6875. For my part, I'd like to see either a full 1.0 release, or at least an rc5 containing this fix.

Your issue can be fixed easily in your app code by explicitly using adapter(window.localStorage) instead of relying on the default store code. You'll probably also need to use HEAD of the 1.0 branch for that to work correctly under mocha.

elgerlambert commented 8 years ago

Merged, thanks!