This commit is contained in:
2020-05-17 13:56:21 +03:00
commit e9a41ffc22
32 changed files with 10191 additions and 0 deletions

16
src/hook/usePersist.js Normal file
View File

@@ -0,0 +1,16 @@
import { useEffect } from 'react';
const DEFAULT_STATE_KEY = 'state';
export const usePersistedContext = (context, key = DEFAULT_STATE_KEY) => {
const persistedContext = localStorage.getItem(key);
return persistedContext ? JSON.parse(persistedContext) : context;
};
export const usePersistedReducer = ([state, dispatch], key = DEFAULT_STATE_KEY) => {
useEffect(
() => localStorage.setItem(key, JSON.stringify(state)),
[state],
);
return [state, dispatch];
};