We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 945436d commit 1280c7fCopy full SHA for 1280c7f
1 file changed
index.js
@@ -1200,16 +1200,22 @@ export function useSessionStorage(key, initialValue) {
1200
[key, store]
1201
);
1202
1203
+ const initialValueRef = useRef(initialValue)
1204
+
1205
React.useEffect(() => {
1206
if (
1207
getSessionStorageItem(key) === null &&
1208
typeof initialValue !== "undefined"
1209
) {
- setSessionStorageItem(key, initialValue);
1210
+ setSessionStorageItem(key, initialValueRef.current);
1211
}
- }, [key, initialValue]);
1212
+ }, [key]);
1213
- return [store ? JSON.parse(store) : initialValue, setState];
1214
+ const result = useMemo(() => {
1215
+ return store ? JSON.parse(store) : initialValueRef.current
1216
+ }, [store])
1217
1218
+ return [result, setState];
1219
1220
1221
export function useSet(values) {
0 commit comments