0

我想根据组件卸载更新我的 redux 状态,但是如果用户终止应用程序,如何检测它并根据应用程序终止更新我的 redux 状态,因为在退出应用程序的情况下不会调用 componentWillUnmount。有没有办法在本机反应中检测应用程序终止(未暂停)?

4

1 回答 1

0

我想你的困惑应该通过理解以下方法来消除:

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

于 2021-04-12T16:41:04.480 回答