Gatsby: Best way to trigger a site init function in GatsbyJS?

Created on 13 May 2019  路  2Comments  路  Source: gatsbyjs/gatsby

Hi, I need to be able to init a page based off cookie settings.

So back in the day you could just do a <body onload="func()" />

I've seen different ways to do this in React with props and querying the virtual DOM, but what is the best way to do this in GatsbyJS in particular?

Right now I just have a quick hack with <body onLoad={myFunc()} /> and then I set a bool to return every time a link is clicked.

question or discussion

Most helpful comment

Depending on when exactly you want to get your callback called you can use one of those Gatsby browser APIs:

I'm not sure what you want to do there, so if those won't work for you, please add more information so we talk about specific use case.

All 2 comments

Depending on when exactly you want to get your callback called you can use one of those Gatsby browser APIs:

I'm not sure what you want to do there, so if those won't work for you, please add more information so we talk about specific use case.

Thanks a lot this should be useful. Haven't had time to try it, but seems like it should work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benstr picture benstr  路  3Comments

jimfilippou picture jimfilippou  路  3Comments

ferMartz picture ferMartz  路  3Comments

KyleAMathews picture KyleAMathews  路  3Comments

signalwerk picture signalwerk  路  3Comments