@heymarco/next-auth
Version:
A complete authentication solution for web applications.
37 lines (36 loc) • 1.08 kB
JavaScript
// react:
import {
// react:
default as React, } from 'react';
// contexts:
const contextMap = new Map();
export const createContext = (defaultValue) => {
const contextStack = [];
const context = {
defaultValue,
Provider: (props) => {
// jsx:
const ContextEnter = () => {
contextStack.push(props.value);
return null;
};
const ContextExit = () => {
contextStack.pop();
return null;
};
return (React.createElement(React.Fragment, null,
React.createElement(ContextEnter, null),
props.children,
React.createElement(ContextExit, null)));
},
};
contextMap.set(context, contextStack);
return context;
};
// hooks:
export const useContext = (context) => {
const contextStack = contextMap.get(context);
if ((contextStack !== undefined) && !!contextStack.length)
return contextStack[contextStack.length - 1];
return context.defaultValue;
};