Download react-use/src/useCookie.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 713 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-use/src/useCookie.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/react-use/src/useCookie.ts
-
curl -L -o useCookie.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-use/src/useCookie.ts
713 Bytes
| import { useCallback, useState } from 'react'; | |
| import Cookies from 'js-cookie'; | |
| const useCookie = ( | |
| cookieName: string | |
| ): [string | null, (newValue: string, options?: Cookies.CookieAttributes) => void, () => void] => { | |
| const [value, setValue] = useState<string | null>(() => Cookies.get(cookieName) || null); | |
| const updateCookie = useCallback( | |
| (newValue: string, options?: Cookies.CookieAttributes) => { | |
| Cookies.set(cookieName, newValue, options); | |
| setValue(newValue); | |
| }, | |
| [cookieName] | |
| ); | |
| const deleteCookie = useCallback(() => { | |
| Cookies.remove(cookieName); | |
| setValue(null); | |
| }, [cookieName]); | |
| return [value, updateCookie, deleteCookie]; | |
| }; | |
| export default useCookie; | |