-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ON-41217 # Added useLoadDataEffect hook
- Loading branch information
1 parent
be896a0
commit 909159c
Showing
4 changed files
with
65 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as React from 'react' | ||
|
||
/** | ||
* This function is a react hook for loading data and reloading it with a way of | ||
* aborting the request. Useful when using reducers to load data asynchronously | ||
* and store the state outside of this react hook. | ||
* | ||
* ## Example | ||
* | ||
* ```js | ||
* import { useLoadDataEffect } from '@oneblink/apps-react' | ||
* | ||
* const fetchData = async (abortSignal) => { | ||
* const response = await fetch( | ||
* `https://some-website.com/api?data=data`, | ||
* { | ||
* signal: abortSignal, | ||
* }, | ||
* ) | ||
* | ||
* const data = await response.json() | ||
* // store data in a data layer for later use | ||
* } | ||
* | ||
* export default function MyComponent() { | ||
* const handleRefresh = useLoadDataEffect(fetchData) | ||
* | ||
* return <button onClick={handleRefresh}>Refresh</button> | ||
* } | ||
* ``` | ||
* | ||
* @param onLoad The function that fetches your data and stores it for later | ||
* use. | ||
* @returns A function to reload the data | ||
* @group Hooks | ||
*/ | ||
export default function useLoadDataEffect( | ||
onLoad: (abortSignal: AbortSignal) => void, | ||
): () => void { | ||
// We use a number to trigger the refresh function so that | ||
// we can pass an abort controller using a useEffect and | ||
// have it aborted if the refresh function is triggered again. | ||
const [loadCount, setLoadCount] = React.useState(0) | ||
|
||
const handleReload = React.useCallback(() => { | ||
setLoadCount((currentLoadCount) => currentLoadCount + 1) | ||
}, []) | ||
|
||
React.useEffect(() => { | ||
const abortController = new AbortController() | ||
onLoad(abortController.signal) | ||
return () => { | ||
abortController.abort() | ||
} | ||
}, [loadCount, onLoad]) | ||
|
||
return handleReload | ||
} |
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