arcx
Version:
A lightweight, dependency-free fetch utility for APIs and React.
37 lines (36 loc) • 1.22 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArcXContext = void 0;
exports.ArcXProvider = ArcXProvider;
exports.useArcX = useArcX;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const config_1 = require("./config");
/**
* Context for ArcX configuration in React.
*/
exports.ArcXContext = (0, react_1.createContext)(null);
/**
* A React provider component that automatically calls `configureArcX`
* with the props you pass in. Ideal for Next.js or React applications.
*
* @param props - ArcXConfig plus children for the provider.
*/
function ArcXProvider({ children, ...config }) {
(0, react_1.useEffect)(() => {
// Merge the passed config into the globalConfig
(0, config_1.configureArcX)(config);
}, [config]);
return (0, jsx_runtime_1.jsx)(exports.ArcXContext.Provider, { value: config, children: children });
}
/**
* A convenience hook to retrieve ArcX's config from context, if needed.
*/
function useArcX() {
const ctx = (0, react_1.useContext)(exports.ArcXContext);
if (!ctx) {
throw new Error("useArcX must be used within an ArcXProvider.");
}
return ctx;
}