-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
21 changed files
with
1,272 additions
and
728 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
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,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# `useError` | ||
|
||
React side-effect hook that returns an error dispatcher. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { useError } from 'react-use'; | ||
|
||
const Demo = () => { | ||
const dispatchError = useError(); | ||
|
||
const clickHandler = () => { | ||
dispatchError(new Error('Some error!')); | ||
}; | ||
|
||
return <button onClick={clickHandler}>Click me to throw</button>; | ||
}; | ||
|
||
// In parent app | ||
const App = () => ( | ||
<ErrorBoundary> | ||
<Demo /> | ||
</ErrorBoundary> | ||
); | ||
``` | ||
|
||
## Reference | ||
|
||
```js | ||
const dispatchError = useError(); | ||
``` | ||
|
||
- `dispatchError` — Callback of type `(err: Error) => void` |
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
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,19 @@ | ||
import { useState, useEffect, useCallback } from 'react'; | ||
|
||
const useError = (): ((err: Error) => void) => { | ||
const [error, setError] = useState<Error | null>(null); | ||
|
||
useEffect(() => { | ||
if (error) { | ||
throw error; | ||
} | ||
}, [error]); | ||
|
||
const dispatchError = useCallback((err: Error) => { | ||
setError(err); | ||
}, []); | ||
|
||
return dispatchError; | ||
}; | ||
|
||
export default useError; |
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
Oops, something went wrong.