arcx
Version:
A lightweight, dependency-free fetch utility for APIs and React.
32 lines (31 loc) • 1.01 kB
JavaScript
"use client";
import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect } from "react";
import { configureArcX } from "./config";
/**
* Context for ArcX configuration in React.
*/
export const ArcXContext = 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.
*/
export function ArcXProvider({ children, ...config }) {
useEffect(() => {
// Merge the passed config into the globalConfig
configureArcX(config);
}, [config]);
return _jsx(ArcXContext.Provider, { value: config, children: children });
}
/**
* A convenience hook to retrieve ArcX's config from context, if needed.
*/
export function useArcX() {
const ctx = useContext(ArcXContext);
if (!ctx) {
throw new Error("useArcX must be used within an ArcXProvider.");
}
return ctx;
}