当导航更改为本机时,取消所有异步功能

时间:2018-03-31 20:13:38

标签: javascript reactjs react-native

加载此组件时,startSync函数启动。但是,我还有一个exit函数,如果在任何一个期间按下,它会导航到登录屏幕。

但是,如果按下退出按钮,它会将我带回登录页面。但是,异步功能仍在运行,因此当它完成时,它将导航到Identification屏幕。

我想知道是否有办法取消所有异步功能,以便在按下退出按钮后没有任何东西在后台运行。

import React, { Component } from 'react';
import { ActivityIndicator, AsyncStorage, Button, StatusBar, Text, StyleSheet, View, } from 'react-native';
import * as pouchDB_helper from '../utils/pouchdb';

type Props = {};
export default class SyncScreen extends Component<Props> {

  startSync = async () => {

    pouchDB_helper.sync().then((response) => {

      AsyncStorage.setItem('initial_sync', 'true');

      //navigate to the identification page
      this.props.navigation.navigate('Identification');

    }, (error) => { Alert.alert("Error", "Syncing failed. Please try again."); });

  }

  exit = async () => {
    await AsyncStorage.clear();
    this.props.navigation.navigate('Login');
  }

  componentDidMount() {
    this.startSync();
  }

  static navigationOptions = {
    title: 'Syncing Settings',
  };

  render() {
    return (
      <View style={styles.container}>
          <Text style={styles.footerText} onPress={this.exit}>Exit</Text>
      </View>
    );
  }
}

0 个答案:

没有答案