Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Rex Dri
Rex Dri
Commits
3ea4525d
Verified
Commit
3ea4525d
authored
Apr 11, 2020
by
Florent Chehab
Browse files
fix(usePersistentState): optmized operations
parent
c9a0d157
Changes
1
Hide whitespace changes
Inline
Side-by-side
frontend/src/hooks/usePersistentState.js
View file @
3ea4525d
...
...
@@ -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
g
lobalValue
=
globalState
.
get
(
key
);
const
currentG
lobalValue
=
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
]
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment