Verified Commit 3ea4525d authored by Florent Chehab's avatar Florent Chehab
Browse files

fix(usePersistentState): optmized operations

parent c9a0d157
......@@ -4,6 +4,10 @@ import useConstantState from "./useConstantState";
export const globalState = new Map();
if (process.env.NODE_ENV !== "production") {
window.globalRexDriState = globalState;
}
/**
* Gets the latest data retrieved from the api for the key
* @param {string} key - route / sing or plur
......@@ -40,12 +44,15 @@ function usePersistentState(key, initialValue) {
const setValue = useCallback(
value => {
const globalValue = globalState.get(key);
const currentGlobalValue = globalState.get(key);
const valueToStore =
value instanceof Function ? value(globalValue) : value;
// Save state
globalState.set(key, valueToStore);
setStoredValue(valueToStore);
value instanceof Function ? value(currentGlobalValue) : value;
if (currentGlobalValue !== valueToStore) {
// Save state
globalState.set(key, valueToStore);
setStoredValue(valueToStore);
}
},
[globalState]
);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment