0

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?

Prince Mittal
  • 326
  • 3
  • 15
  • This might help you https://stackoverflow.com/questions/38962034/how-to-detect-when-a-react-native-app-is-closed-not-suspended#:~:text=As%20a%20simple%20method%20%2C%20we,detect%20the%20app%20is%20closed. – Ragul CS Apr 12 '21 at 12:44
  • @RagulCs That's the question I need the answer to as well but I didn't find any proper solution there as well. I will update my question. – Prince Mittal Apr 12 '21 at 17:55
  • @3limin4t0r I will tell you the use case I have I want to display a modal on first-time component mounts and I am maintaining that in my redux state and I am toggling it to false on componentWillUnmount and as well as on modal dismiss. But assume a user opened the component and modal get displayed at that point user quits the app then next time the modal will be shown again as it's state is not updated. – Prince Mittal Apr 12 '21 at 18:23

1 Answers1

0

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>
    );
  }
}
yash sanghavi
  • 368
  • 3
  • 5
  • I understand how the lifecycle method works in react and react native what I want to know is specific to react-native instead of react. That when we quit an app will the componentWillUnmount be called if not is there any way to know that the user is quitting the app. – Prince Mittal Apr 12 '21 at 17:49