Gathering detailed insights and metrics for @cullenbond/use-local-storage-sync
Gathering detailed insights and metrics for @cullenbond/use-local-storage-sync
Gathering detailed insights and metrics for @cullenbond/use-local-storage-sync
Gathering detailed insights and metrics for @cullenbond/use-local-storage-sync
Save state to local storage and sync when updates are made to local storage
npm install @cullenbond/use-local-storage-sync
Typescript
Module System
Node Version
NPM Version
TypeScript (71.6%)
JavaScript (28.4%)
Built with Next.js • Fully responsive • SEO optimized • Open source ready
Total Downloads
777
Last Day
2
Last Week
6
Last Month
6
Last Year
210
MIT License
3 Stars
16 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Mar 01, 2024
Latest Version
1.0.7
Package Id
@cullenbond/use-local-storage-sync@1.0.7
Unpacked Size
6.47 kB
Size
2.88 kB
File Count
6
NPM Version
8.19.2
Node Version
19.0.1
Published on
Mar 01, 2024
Cumulative downloads
Total Downloads
Last Day
0%
2
Compared to previous day
Last Week
0%
6
Compared to previous week
Last Month
-70%
6
Compared to previous month
Last Year
-63%
210
Compared to previous year
3
npm install @cullenbond/use-local-storage-sync
import { useLocalStorageSync } from "@cullenbond/use-local-storage-sync";
const [showButtons, setShowButtons] = useLocalStorageSync("showButtons", false);
const [persistedCount, setPersistedCount] = useLocalStorageSync("persistedCount", 0);
const [userId, setUserId] = useLocalStorageSync("userId", "bob");
const [groceryList, setGroceryList] = useLocalStorageSync("groceryList", ["Apples", "Oranges", "Bananas"]);
An abstract example of load logic using initialization value, to avoid infinite load loops. If you persist and load data to data stores bi-directionally.
const [showMenu, setShowMenu, showMenuError, showMenuInitialized] = useLocalStorageSync("showSettingsMenu", true)
useEffect(()=>{
if(showMenuInitialized){
// do things that might otherwise cause side effects
// update redux store to localStorage value, after initialization
}
},[showMenu])
useEffect(()=>{
if(showMenuInitialized){
// do things that might otherwise cause side effects
// update localStorage value, after initialization and when state is updated
setShowMenu(state.showMenu)
}
},[state.showMenu])
Similar to how JS Map() works we will identify the name of our value that is to be stored.
It is important to note that if you might to be persisting several variations of a name, to make sure the key name is unique.
Example:
const [buttonClickCount, setButtonClickCount] = useLocalStorageSync("buttonClickCount")
2. Default Initial Value
The initial value takes precedence if no existing values have been stored locally.
Example:
const [buttonClickCount, setButtonClickCount] = useLocalStorageSync("buttonClickCount", 0)
3. (Optional) getItem function
Method called when retrieving a stored value, before setting value to be used in state.
The hook will json.parse() the stored value, by default
Example:
const [buttonClickCount, setButtonClickCount] = useLocalStorageSync("buttonClickCount", 0, value => JSON.parse(value))
4. (Optional) setItem function
Method called when setting a stored or synced value before setting the value.
The hook will json.stringify() the stored value, by default
Example:
const [buttonClickCount, setButtonClickCount] = useLocalStorageSync("buttonClickCount", 0, value => JSON.parse(value), value => JSON.stringify(value))
No vulnerabilities found.