@ace-fetch/react
Version:
react adapter for @ace-fetch/core.
33 lines (32 loc) • 898 B
JavaScript
import { createContext, useContext } from 'react';
/**
* activeFetch must be called to handle SSR at the top of functions like
* `fetch`, `setup`, `serverPrefetch` and others
*/
export var activeFetch;
/**
* fetch context
*/
export var FetchContext = createContext({});
/**
* Set or unset active fetch, Used in SSR and internally when calling
* actions and getters
* @param fetch Fetch instance
*/
export var setActiveFetch = function (fetch) { return (activeFetch = fetch); };
/**
* get fetch from context
*/
export var getContextFetch = function () {
try {
// would throw an error if call outside of the body of a function component
return useContext(FetchContext).fetch;
}
catch (_a) {
return;
}
};
/**
* Get the currently active fetch if there is any.
*/
export var getActiveFetch = function () { return getContextFetch() || activeFetch; };