简体   繁体   English

如何从移动浏览器修复 React js 的事件监听器问题

[英]How to fix event listener issues of react js from mobile browser

I have a react js app.我有一个反应 js 应用程序。 And it has one solidity event listener Listener works fine the first time I use the app它有一个solidity事件监听器监听器在我第一次使用该应用程序时工作正常

But when I press home button or go to another app from the browser and reopen the browser after sometimes(1/2 minutes), the listener stops listening and it only starts listening again after refreshing the app.但是当我从浏览器按主页按钮或 go 到另一个应用程序并在有时(1/2 分钟)后重新打开浏览器时,侦听器停止侦听,只有在刷新应用程序后才重新开始侦听。

So is there any solution with that I can handle it?那么有什么我可以处理的解决方案吗?

My code我的代码

useEffect(() => {
    let isMounted = true
    
    if (isMounted && props.blockchain.smartContract != null && props.blockchain.smartContract != undefined && props.users.length != 0) {

      props.blockchain.smartContract.events.Transfer({}).on("connected", function(subscriptionId){
        console.log(subscriptionId);
        console.log("Subscription ID")
    }).on('data', event => {

          
          let item = { key:event.transactionHash,sender: event.returnValues.from, recipient: event.returnValues.to, value: event.returnValues.value / (10 ** 18), type_id: '1', interval: '1s' }
          
          setFeed(item,event)
          
          console.log(event.transactionHash)
         


        })
        .on('changed', function(event){
            // remove event from local database
            
            
        })
        .on('error', function(error, receipt) { // If the transaction was rejected by the network with a receipt, the second parameter will be the receipt.
            console.log(error)
            setCointEvent(Math.random() * 100)
        });
    }
    return (()=>{
      isMounted = false
      
    })
  }, [props.blockchain.smartContract, props.users,coinEvent]);

Does react js hooks understand if user closes the browser?如果用户关闭浏览器,反应 js 钩子是否理解?

window.onload = function (){ //your function here // maybe it works. window.onload = function (){ //你的 function 在这里 // 也许它有效。 It works in everys JS file perfectly.它完美地适用于每个 JS 文件。

//It will allow your functions to happen from page loads. //它将允许您的功能在页面加载时发生。

//you can add listener here it will listen. //你可以在这里添加监听器它会监听。 } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM