简体   繁体   中英

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. Is there a way to detect app termination(not suspended) in react native?

I Guess Your Confusion Should Remove By Understand Following Approche:

 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> ); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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