Created
March 26, 2021 03:50
-
-
Save imoby/900a501747cee63364e6f3552be0cd50 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
import React, { createContext, useMemo, useState } from 'react'; | |
import { Mixpanel } from 'mixpanel-react-native'; | |
import { mixpanelToken } from '../../app.json'; | |
type MixpanelContextType = { | |
mixpanel: Mixpanel | undefined; | |
}; | |
const mixpanelContextDefault: MixpanelContextType | undefined = { | |
mixpanel: undefined, | |
}; | |
export const MixpanelContext = createContext<MixpanelContextType>( | |
mixpanelContextDefault | |
); | |
export const MixpanelProvider = ({ children }) => { | |
const [mixpanel, setMixpanel] = useState<Mixpanel | undefined>( | |
mixpanelContextDefault.mixpanel | |
); | |
React.useEffect(() => { | |
const initMixpanel = async () => { | |
const initializedMixpanel = await Mixpanel.init(mixpanelToken); | |
setMixpanel(initializedMixpanel); | |
}; | |
initMixpanel(); | |
}, []); | |
const value = useMemo( | |
() => ({ | |
mixpanel, | |
}), | |
[mixpanel] | |
); | |
return ( | |
<MixpanelContext.Provider value={value}> | |
{children} | |
</MixpanelContext.Provider> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment