Skip to content

Commit

Permalink
[@mantine/hooks] use-local-storage: Fix inconsistent default value pe…
Browse files Browse the repository at this point in the history
…rsistence if `getInitialValueInEffect` is set (#5796)
  • Loading branch information
rtivital committed Feb 26, 2024
1 parent 5f1d07d commit f8ec3f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useEffect } from 'react';
import { readLocalStorageValue, useLocalStorage } from './use-local-storage';

export default { title: 'use-local-storage' };

const key = Math.random().toString(36).substr(2, 5);

export function Usage() {
const [id] = useLocalStorage({
defaultValue: 123,
getInitialValueInEffect: false,
key,
});

const [storedValue, setStoredValue] = React.useState(null);

useEffect(() => {
setTimeout(() => {
setStoredValue(readLocalStorageValue({ key }));
}, 20);
}, []);

return (
<div style={{ padding: 40 }}>
<p>Hook value: {id}</p>
<p>Local storage value: {storedValue}</p>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ export function createStorage<T>(type: StorageType, hookName: string) {
}, [defaultValue, value, setStorageValue]);

useEffect(() => {
if (getInitialValueInEffect) {
setStorageValue(readStorageValue());
}
setStorageValue(readStorageValue());
}, []);

return [value === undefined ? defaultValue : value, setStorageValue, removeStorageValue] as [
Expand Down

0 comments on commit f8ec3f1

Please sign in to comment.