Home > Blockchain >  Gatsby.js adding a global function to checkUserSession
Gatsby.js adding a global function to checkUserSession

Time:04-13

I am wondering how to set a global function to be rendered in all gatsby pages, by using create-react-app I used to do that in the App.js file,but now I don't know how to manipulate it with using Gatsby this is what I want to implement :

const dispatch = useDispatch()
useEffect(()=>{
  dispatch(checkUserSession())
}, [])

CodePudding user response:

You can use one of the multiple Gatsby Browser APIs. In your case, onRouteUpdate should do the trick. Add in the gatsby-browser.js the following:

exports.onRouteUpdate = () => {
  dispatch(checkUserSession())
}

onRouteUpdate is called when the user changes routes, including on the initial load of the app

  • Related