2021-05-11 14:38:56 +00:00
|
|
|
import useBackend from "./useBackend"
|
2021-05-07 23:40:49 +00:00
|
|
|
import { useEffect } from "react"
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-05-11 14:38:56 +00:00
|
|
|
* Like {@link useBackend}, but runs as soon as the component is rendered.
|
2021-05-07 23:40:49 +00:00
|
|
|
*
|
|
|
|
* @param fetchData - The function to use when fetching data.
|
|
|
|
* @param method - The HTTP method to use.
|
|
|
|
* @param path - The HTTP path to fetch the data at.
|
|
|
|
* @param body - The body of the HTTP request (it will be JSONified before being sent).
|
|
|
|
* @param init - Additional `init` parameters to pass to `fetch`.
|
2021-05-19 17:56:41 +00:00
|
|
|
* @deprecated since 2021-05-19
|
2021-05-07 23:40:49 +00:00
|
|
|
*/
|
2021-05-11 14:38:56 +00:00
|
|
|
export default function useBackendImmediately(fetchData, method, path, body, init) {
|
|
|
|
const { data, error, loading, fetchNow } = useBackend(fetchData, method, path, body, init)
|
2021-05-07 23:40:49 +00:00
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() => {
|
2021-05-11 14:37:15 +00:00
|
|
|
if(!(
|
|
|
|
loading || data || error
|
|
|
|
)) {
|
2021-05-07 23:40:49 +00:00
|
|
|
fetchNow()
|
|
|
|
}
|
|
|
|
},
|
2021-05-11 14:37:15 +00:00
|
|
|
[data, error, loading, fetchNow],
|
2021-05-07 23:40:49 +00:00
|
|
|
)
|
|
|
|
|
2021-05-11 14:37:15 +00:00
|
|
|
return { data, error, loading, fetchNow }
|
2021-05-07 23:40:49 +00:00
|
|
|
}
|