Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Last active October 15, 2020 02:04
Show Gist options
  • Save codeBelt/2d11e48abf70429036a9fc1fe6fd37e8 to your computer and use it in GitHub Desktop.
Save codeBelt/2d11e48abf70429036a9fc1fe6fd37e8 to your computer and use it in GitHub Desktop.
export const PortalContext = React.createContext(null);
export const PortalProvider = (props) => {
const [portalMap, setPortalMap] = useState(new Map());
const addPortalItem = useCallback((portalType, component) => {
portalMap.set(portalType, component);
const clonedMapWithNewItem = new Map(portalMap);
setPortalMap(clonedMapWithNewItem);
}, []);
const removePortalItem = useCallback((portalType) => {
portalMap.delete(portalType);
const clonedMapWithoutItem = new Map(portalMap);
setPortalMap(clonedMapWithoutItem);
}, []);
return (
<PortalContext.Provider value={{ portalMap, addPortalItem, removePortalItem }}>
{props.children}
</PortalContext.Provider>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment