Created
April 29, 2020 16:52
-
-
Save enaluz/13d79d4a50963c4cd16fa67f3ead444e 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 from "react"; | |
import { GlobalContextProvider } from "./GlobalContext"; | |
import AppNavigator from "./AppNavigator"; | |
import { NavigationContainer } from "@react-navigation/native"; | |
const Root = () => ( | |
<NavigationContainer> | |
<GlobalContextProvider> | |
<AppNavigator /> | |
</GlobalContextProvider> | |
</NavigationContainer> | |
) | |
export default Root; |
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, { useEffect, useState } from "react"; | |
type ContextProps = { | |
state: State; | |
}; | |
const GlobalContext = React.createContext<ContextProps>( | |
(null as unknown) as ContextProps | |
); | |
interface State { | |
jwt: string | |
} | |
const initialState: State = { jwt: "" }; | |
interface Props { children: React.ReactNode } | |
const GlobalContextProvider = (props: Props) => { | |
const [state, setState] = useState(initialState); | |
useEffect(() => { | |
(async () => { | |
// If you want to also store it in async storage, | |
// you could always access it here and set the jwt in state | |
})() | |
}) | |
return ( | |
<GlobalContext.Provider | |
value={{ state }} | |
> | |
{props.children} | |
</GlobalContext.Provider> | |
); | |
} | |
export { GlobalContext, GlobalContextProvider }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment