@ace-fetch/react
Version:
react adapter for @ace-fetch/core.
34 lines (33 loc) • 1.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getActiveFetch = exports.getContextFetch = exports.setActiveFetch = exports.FetchContext = exports.activeFetch = void 0;
var react_1 = require("react");
/**
* fetch context
*/
exports.FetchContext = (0, react_1.createContext)({});
/**
* Set or unset active fetch, Used in SSR and internally when calling
* actions and getters
* @param fetch Fetch instance
*/
var setActiveFetch = function (fetch) { return (exports.activeFetch = fetch); };
exports.setActiveFetch = setActiveFetch;
/**
* get fetch from context
*/
var getContextFetch = function () {
try {
// would throw an error if call outside of the body of a function component
return (0, react_1.useContext)(exports.FetchContext).fetch;
}
catch (_a) {
return;
}
};
exports.getContextFetch = getContextFetch;
/**
* Get the currently active fetch if there is any.
*/
var getActiveFetch = function () { return (0, exports.getContextFetch)() || exports.activeFetch; };
exports.getActiveFetch = getActiveFetch;