Skip to content

Commit

Permalink
Merge pull request #42 from maliksenpai/master
Browse files Browse the repository at this point in the history
Added remove cookie function
  • Loading branch information
tylerwolff authored May 6, 2024
2 parents d5f13e8 + b366a2f commit 4c52058
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ declare module 'react-use-cookie' {

export function getCookie(name: string, initialValue?: string): string;

export function removeCookie(name: string): void;

export default function (
key: string,
initialValue?: string
): [string, updateItem];
): [string, updateItem, removeItem];
}
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,24 @@ export const getCookie = (name, initialValue = '') => {
);
};

export const removeCookie = (name) => {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
}

export default function (key, initialValue) {
const [item, setItem] = useState(() => {
return getCookie(key, initialValue);
});

const removeItem = useCallback(() => {
setItem(undefined);
removeCookie(key);
}, [key]);

const updateItem = useCallback((value, options) => {
setItem(value);
setCookie(key, value, options);
}, [key]);

return [item, updateItem];
return [item, updateItem, removeItem];
}

0 comments on commit 4c52058

Please sign in to comment.