react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
43 lines • 1.09 kB
JavaScript
import react from "react";
const cacheFallback = () => () => ({ value: void 0 });
const createServerContext = (defaultValue) => {
throwInClient();
const cache = react.cache ?? cacheFallback;
const getCache = cache(() => ({
value: void 0
}));
const Provider = ({
children,
value
}) => {
getCache().value = value;
return children;
};
const ServerContext = Provider;
ServerContext.Provider = Provider;
ServerContext.Consumer = (props) => {
const store = getCache();
return props.children(store ? store.value : defaultValue);
};
ServerContext._storage = getCache;
ServerContext._defaultValue = defaultValue;
return ServerContext;
};
const getServerContext = ({
_storage,
_defaultValue
}) => {
const store = _storage();
if (!store) return _defaultValue;
return store.value;
};
const throwInClient = () => {
if (typeof window !== "undefined") {
throw new Error(`createServerContext only works in Server Components`);
}
};
export {
createServerContext,
getServerContext
};
//# sourceMappingURL=serverContext.mjs.map