簡體   English   中英

React Native 檢測用戶何時嘗試退出應用程序?

[英]React Native detect when user is trying to quit the app?

I want to update my redux state based on the component unmount but in case a user terminates the app how to detect that and update my redux state based on the app termination as the componentWillUnmount will not be called in case of quitting the app. 有沒有辦法在本機反應中檢測應用程序終止(未暫停)?

我猜你的困惑應該通過理解以下方法來消除:

 import React from "react"; export default class Clock extends React.Component { constructor(props) { console.log("Clock", "constructor"); super(props); this.state = { date: new Date() }; } tick() { this.setState({ date: new Date() }); } // These methods are called "lifecycle hooks". componentDidMount() { console.log("Clock", "componentDidMount"); this.timerID = setInterval(() => { this.tick(); }, 1000); } // These methods are called "lifecycle hooks". componentWillUnmount() { console.log("Clock", "componentWillUnmount"); clearInterval(this.timerID); } render() { return ( <div>It is {this.state.date.toLocaleTimeString()}.</div> ); } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM