简体   繁体   English

Gatsby.js 添加全局 function 到 checkUserSession

[英]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.

相关问题 Mocha.js - 如何保存全局变量? - Mocha.js - How to save a global variable? Spring Cloud Function on AWS Lambda Platform 全局异常处理 - Global Exception Handling in Spring Cloud Function on AWS Lambda Platform 触发器主体属性的全局参数和在 @triggerbody().folderpath 上使用 split function - Global parameters for trigger body properties and using split function on @triggerbody().folderpath InvalidDBClusterStateFault:源集群位于 state 中,在全局集群中添加新的 rds 集群时,它对物理复制无效 - InvalidDBClusterStateFault: Source cluster is in a state which is not valid for physical replication when adding a new rds cluster in global cluster Cloud Function Firebase 添加内购的解决方法 - Cloud Function Firebase solution to adding in app purchases 添加Pyjwt到aws lambda function层 - Adding Pyjwt to aws lambda function layer Powershell 脚本:为什么这个 function 无法访问全局变量 - Powershell scripting : Why is this function not able to access a global variable 在完成处理程序中添加数据后显示为空的全局数组 (Swift) - Global array showing as empty after adding data in a completion handler (Swift) 异步 Function 在 React 中向数组添加元素两次 - Async Function Adding Elements Twice to an Array in React Firebase 云 function cors 即使添加 cors 中间件也会出错 - Firebase cloud function cors error even when adding cors middleware
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM