mirror of
https://github.com/balzack/databag.git
synced 2025-02-15 21:19:16 +00:00
45 lines
846 B
JavaScript
45 lines
846 B
JavaScript
import { useEffect, useState } from 'react';
|
|
|
|
export function useStoreContext() {
|
|
|
|
const [state, setState] = useState({});
|
|
|
|
const resetState = () => {
|
|
setState((s) => {
|
|
localStorage.setItem('store', JSON.stringify({}));
|
|
return {}
|
|
});
|
|
};
|
|
|
|
const updateState = (value) => {
|
|
setState((s) => {
|
|
const store = { ...s, ...value };
|
|
localStorage.setItem('store', JSON.stringify(store));
|
|
return store;
|
|
});
|
|
};
|
|
|
|
useEffect(() => {
|
|
const store = localStorage.getItem('store');
|
|
if (store != null) {
|
|
updateState({ ...JSON.parse(store) });
|
|
}
|
|
}, []);
|
|
|
|
const actions = {
|
|
clear: () => {
|
|
resetState();
|
|
},
|
|
setValue: (key, value) => {
|
|
updateState({ [key]: value });
|
|
},
|
|
getValue: (key) => {
|
|
return state[key];
|
|
}
|
|
}
|
|
|
|
return { state, actions }
|
|
}
|
|
|
|
|