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

363 views Asked by At

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?

1

There are 1 answers

1
yash sanghavi On

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