-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# `useCookie` | ||
|
||
React hook that returns the current value of a `cookie`, a callback to update the `cookie` | ||
and a callback to delete the `cookie.` | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { useCookie } from "react-use"; | ||
|
||
const Demo = () => { | ||
const [value, updateCookie, deleteCookie] = useCookie("my-cookie"); | ||
const [counter, setCounter] = useState(1); | ||
|
||
useEffect(() => { | ||
deleteCookie(); | ||
}, []); | ||
|
||
const updateCookieHandler = () => { | ||
updateCookie(`my-awesome-cookie-${counter}`); | ||
setCounter(c => c + 1); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<p>Value: {value}</p> | ||
<button onClick={updateCookieHandler}>Update Cookie</button> | ||
<br /> | ||
<button onClick={deleteCookie}>Delete Cookie</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
const [value, updateCookie, deleteCookie] = useCookie(cookieName: string); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useState, useCallback } 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { storiesOf } from "@storybook/react"; | ||
import React, { useState, useEffect } from "react"; | ||
import { useCookie } from "../src"; | ||
import ShowDocs from "./util/ShowDocs"; | ||
|
||
const Demo = () => { | ||
const [value, updateCookie, deleteCookie] = useCookie("my-cookie"); | ||
const [counter, setCounter] = useState(1); | ||
|
||
useEffect(() => { | ||
deleteCookie(); | ||
}, []); | ||
|
||
const updateCookieHandler = () => { | ||
updateCookie(`my-awesome-cookie-${counter}`); | ||
setCounter(c => c + 1); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<p>Value: {value}</p> | ||
<button onClick={updateCookieHandler}>Update Cookie</button> | ||
<br /> | ||
<button onClick={deleteCookie}>Delete Cookie</button> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf("Side effects|useCookie", module) | ||
.add("Docs", () => <ShowDocs md={require("../docs/useCookie.md")} />) | ||
.add("Demo", () => <Demo />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import Cookies from 'js-cookie'; | ||
import { useCookie } from '../src'; | ||
|
||
const setup = (cookieName: string) => renderHook(() => useCookie(cookieName)); | ||
|
||
it('should have initial value of null if no cookie exists', () => { | ||
const { result } = setup('some-cookie'); | ||
|
||
expect(result.current[0]).toBeNull(); | ||
}); | ||
|
||
it('should have initial value of the cookie if it exists', () => { | ||
const cookieName = 'some-cookie'; | ||
const value = 'some-value'; | ||
Cookies.set(cookieName, value); | ||
|
||
const { result } = setup(cookieName); | ||
|
||
expect(result.current[0]).toBe(value); | ||
|
||
// cleanup | ||
Cookies.remove(cookieName); | ||
}); | ||
|
||
it('should update the cookie on call to updateCookie', () => { | ||
const spy = jest.spyOn(Cookies, 'set'); | ||
|
||
const cookieName = 'some-cookie'; | ||
const { result } = setup(cookieName); | ||
|
||
const newValue = 'some-new-value'; | ||
act(() => { | ||
result.current[1](newValue); | ||
}); | ||
|
||
expect(result.current[0]).toBe(newValue); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith(cookieName, newValue, undefined); | ||
|
||
// cleanup | ||
spy.mockRestore(); | ||
Cookies.remove(cookieName); | ||
}); | ||
|
||
it('should delete the cookie on call to deleteCookie', () => { | ||
const cookieName = 'some-cookie'; | ||
const value = 'some-value'; | ||
Cookies.set(cookieName, value); | ||
|
||
const spy = jest.spyOn(Cookies, 'remove'); | ||
|
||
const { result } = setup(cookieName); | ||
|
||
expect(result.current[0]).toBe(value); | ||
|
||
act(() => { | ||
result.current[2](); | ||
}); | ||
|
||
expect(result.current[0]).toBeNull(); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenLastCalledWith(cookieName); | ||
|
||
// cleanup | ||
spy.mockRestore(); | ||
Cookies.remove(cookieName); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters