React-Native:运行 加载组件时在后台运行的函数

React-Native: run a function in background while component is loaded

在组件中,同时加载列表视图和所有内容。

是否可以 运行 后台函数每隔几分钟重新加载列表视图数据?

如果是, 当用户离开组件时(转到另一个选项卡,iOS),功能会停止吗?

您可以通过在 componentDidMount 中添加一个 setInterval 并在 componentWillUnmount 中清除它来实现。

let interval;
class HelloWorld extends Component {

  componentDidMount() {
    interval = setInterval(() => {

        // do what you want here.

    }, 10000);
  }

  componentWillUnmount() {
    clearInterval(interval);
  }

  render() {
    return (
      <Text>Hello world!</Text>
    );
  }
}