I need to run some functions (eg. Office UI Fabric React's initializeIcons()
) and AXIOS call (eg. retrieve the logged-in user with Context API) only the first time the site is hit, then store the retrieved values in the React Context and make it available to the whole application.
Gatsby is wrapping my pages' content in a Layout, like:
const IndexPage = () =>
<Layout>
Body of Index Page...
</Layout>
const AnotherPage = () =>
<Layout>
Body of Another Page...
</Layout>
with Layout being like:
const Layout = ({ children }) =>
<>
<Header />
<main>{children}</main>
<Footer />
</>
I know where I can NOT put my Context:
around the pages (or it will be executed everytime the page is hit, and also not available to the other pages):
const IndexPage = () => <MyContextProvider> <Layout> Context Available here </Layout> </MyContextProvider>
const AnotherPage = () => <Layout> Context NOT available here </Layout>
in the Layout (or it will be executed every time):
const Layout = ({ children }) => <MyContextProvider> <Header /> <main>{children}</main> <Footer /> </MyContextProvider>
I suppose I need a root <app>
object to surround with my Context Provider, but what's a clean way to achieve that with Gatsby?
Where should I put my Context Provider?