You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a value is initialized through defaultValue, the value is not set in the session storage.
Expected behavior
Even if the set function is not executed, if the value is initialized through defaultValue, the value must be set in the session storage.
To Reproduce
const[state,set]=useStorageState('dummy-key',{defaultValue: 'foo'});// it doesn't stored in session storage until we call set funciton
Possible Solution
constgetValue=useCallback(<T>()=>{constdata=storage.get(key);if(data==null){returndefaultValue;}try{constresult=JSON.parse(data);if(result==null){storage.set(key,JSON.stringify(defaultValue));// add this linereturndefaultValue;}returnresultasT;}catch{// NOTE: JSON 객체가 아닌 경우returndefaultValue;}},[defaultValue,key,storage]);
Additional context
The text was updated successfully, but these errors were encountered:
It works to add code to this line rather than add code to the line you mentioned.
constgetValue=useCallback(<T>()=>{constdata=storage.get(key);if(data==null){storage.set(key,JSON.stringify(defaultValue));// add this linereturndefaultValue;}try{constresult=JSON.parse(data);// ...// ...
But I think we should avoid setting the value inside the getValue function (because it's a side effect)
I think we need a logic to set defaultValue to storage right after component mount, we need to useEffect because this is synchronizing with external system.
Package Scope
Package name: @toss/react
Describe the bug
If a value is initialized through
defaultValue
, the value is not set in thesession storage
.Expected behavior
Even if the set function is not executed, if the value is initialized through
defaultValue
, the value must be set in thesession storage.
To Reproduce
Possible Solution
Additional context
The text was updated successfully, but these errors were encountered: