[英]Gatsby.js adding a global function to checkUserSession
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:我想知道如何设置全局 function 以在所有 gatsby 页面中呈现,通过使用 create-react-app 我曾经在 App.js 文件中这样做,但现在我不知道如何使用 Gatsby 来操作它这就是我想要实现的:
const dispatch = useDispatch()
useEffect(()=>{
dispatch(checkUserSession())
}, [])
You can use one of the multiple Gatsby Browser APIs.您可以使用多个 Gatsby 浏览器 API 之一。 In your case, onRouteUpdate
should do the trick.在您的情况下, onRouteUpdate
应该可以解决问题。 Add in the gatsby-browser.js
the following:在gatsby-browser.js
中添加以下内容:
exports.onRouteUpdate = () => {
checkUserSession()
}
onRouteUpdate
is called when the user changes routes, including on the initial load of the app onRouteUpdate
在用户更改路线时调用,包括在应用程序的初始加载时
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.