Last active
October 15, 2020 02:04
-
-
Save codeBelt/2d11e48abf70429036a9fc1fe6fd37e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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