@zedux/react
Version:
A Molecular State Engine for React
32 lines (31 loc) • 1.57 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AtomProvider = void 0;
const react_1 = __importDefault(require("react"));
const useEcosystem_1 = require("../hooks/useEcosystem");
const utils_1 = require("../utils");
/**
* Provides an atom instance over React context.
*
* Provided atom instances can be consumed in child components via
* `useAtomContext()`. The atom instance can then be passed to other hooks like
* `useAtomValue()` or `useAtomState()` to create a dynamic dependency on the
* consumed instance.
*
* The providing component should typically register at least a static
* dependency on the provided instance via `useAtomInstance()` or manual
* graphing inside `useEffect()`.
*/
const AtomProvider = ({ children, instance, instances }) => {
const ecosystem = (0, useEcosystem_1.useEcosystem)();
if (true /* DEV */ && !instance && !instances) {
throw new Error('Zedux: AtomProvider requires either an `instance` or `instances` prop');
}
const [nextInstance, ...childInstances] = instances || [instance];
const context = (0, utils_1.getReactContext)(ecosystem, nextInstance.template);
return (react_1.default.createElement(context.Provider, { value: nextInstance }, childInstances.length ? (react_1.default.createElement(exports.AtomProvider, { instances: childInstances }, children)) : (children)));
};
exports.AtomProvider = AtomProvider;
;