-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restoring initial state from localStorage, if any? #49
Comments
hi, setGlobalState('foo', localStorage.getItem('foo')); Another way is to initialize Hope it helps. |
Actually, there's an example: https://github.com/dai-shi/react-hooks-global-state/tree/master/examples/13_persistence |
@dai-shi thanks for helping. I got the point, however there is no |
The const { dispatch, useGlobalState, getGlobalState, setGlobalState } = createStore(
reducer,
initialState
);
const saveToLocalStorage = () => {
localStorage.setItem('foo', JSON.stringify(getGlobalState('foo')));
localStorage.setItem('bar', JSON.stringify(getGlobalState('bar')));
};
const loadFromLocalStorage = () => {
setGlobalState('foo', JSON.parse(localStorage.getItem('foo')));
setGlobalState('bar', JSON.parse(localStorage.getItem('bar')));
}; Notice error handling is omitted. |
Sure, I've got the point of loading and saving to local storage (also, bootstrapping from local storage). What is not clear to me is how to update the local storage every time a new action is dispatcher, that is... where to put Is it just another reducer that doesn't alter the state and it just call the |
Oh, sorry. I see your point now. Hm, good question. Without middleware, only way to get notified is the hook. So, I'd do like this: const { dispatch, useGlobalState: useGlobalStateOrig } = createStore(...);
const useGlobalState = (key) => {
const [state, update] = useGlobalStateOrig(key);
useEffect(() => {
localStorage.setItem(key, JSON.stringify(state));
});
return [state, update];
}; If dispatch is the only way to modify the state, you could also wrap dispatch (which should be better than hacking reducer.) const dispatch = (action) => {
dispatchOrig(action);
saveToLocalStorage();
}; |
I understand now, like the last approach. |
https://github.com/dai-shi/react-hooks-global-state#parameters-1 says BTW, we also have this: https://github.com/dai-shi/react-hooks-global-state/wiki/Persistence |
So, I ended up using the following (the example you posted are relative to using a custom reducer and a custom function import { getFromLocalStorage, saveToLocalStorage } from './util';
import { createStore } from 'react-hooks-global-state';
import { reduceReducers } from './util';
const initialState = {
cart: {
items: [],
}
};
// This is the main reducer
const reducer = (state, action) => { /* ... */ };
const { dispatch, useGlobalState } = createStore(
reduceReducers(
reducer,
// This additional reducer doesn't alter the state, it just store it
state => {
saveToLocalStorage(LOCAL_STORAGE_KEY, state, LOCAL_STORAGE_TTL_SECONDS);
return state;
}
),
// Bootstrapping from local storage, returning the initialState if storage is empty
getFromLocalStorage(LOCAL_STORAGE_KEY, initialState)
);
export { useGlobalState }; I didn't used a custom |
Looks good. I think it's totally valid with the current API and the implementation. Please close the issue if you don't have further questions. Thanks. |
Hi, first thanks for this great library!
I'm sharing a global state between components, exporting from the
globalStage.js
:I'm wondering... how to restore the initial state from the
localStorage
object usingcreateStore
?The text was updated successfully, but these errors were encountered: