Skip to content

Commit

Permalink
Merge pull request #3 from muhammadasif-wd/hooks
Browse files Browse the repository at this point in the history
Create useLocalStorage.tsx
  • Loading branch information
muhammadasif-wd authored May 16, 2024
2 parents 645c4c4 + 45c0ce8 commit 6e01500
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions useLocalStorage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React, {useDebugValue, useEffect, useState} from "react";

export const useLocalStorage = <S>(
key: string,
initialState?: S | (() => S),
): [S, React.Dispatch<React.SetStateAction<S>>, () => void] => {
const [state, setState] = useState<S>(() => {
// check if localStorage is available
try {
if (typeof localStorage !== "undefined") {
const item = localStorage.getItem(key);

return item ? parse(item) : (initialState as S);
} else {
console.error("localStorage is not available.");
}
} catch (error) {
console.error("Error accessing localStorage:", error);
}

return typeof initialState === "function" ? (initialState as () => S)() : initialState;
});

useDebugValue(state);
// get item from local storage
useEffect(() => {
try {
if (typeof localStorage !== "undefined") {
const item = localStorage.getItem(key);

if (item) setState(parse(item));
} else {
console.error("localStorage is not available.");
}
} catch (error) {
console.error("Error accessing localStorage:", error);
}
}, [key]);

// set item from local storage
useEffect(() => {
try {
if (typeof localStorage !== "undefined") {
if (
state !== null &&
state !== "" &&
!(Array.isArray(state) && state.length === 0) &&
!(typeof state === "object" && Object.keys(state).length === 0)
) {
localStorage.setItem(key, JSON.stringify(state));
}
} else {
console.error("localStorage is not available.");
}
} catch (error) {
console.error("Error accessing localStorage:", error);
}
}, [key, state]);

// remove item from local storage
const removeItem = () => {
try {
if (typeof localStorage !== "undefined") {
localStorage.removeItem(key);
setState(initialState!);
} else {
console.error("localStorage is not available.");
}
} catch (error) {
console.error("Error accessing localStorage:", error);
}
};

return [state, setState, removeItem];
};

const parse = (value: string) => {
try {
return JSON.parse(value);
} catch {
return value;
}
};

/*
use case 1: const [value, setValue] = useLocalStorage("data", []);
* when you set your item from local storage, then just call the setValue() function.
example: setValue(data as any);
use case 2: const [value] = useLocalStorage("data");
* when you get your local storage item then just call the value variable.
example: value (console.log(value) or use it as you want)
use case 3: const [removeItem] = useLocalStorage("data");
* when you remove your local storage item then just call the removeItem() function.
example: removeItem();
*/

0 comments on commit 6e01500

Please sign in to comment.