0

我正在尝试在运行删除功能确认之前设置警报功能。

这是我的代码。现在这显示了我预期的带有标题、解释和两个按钮的警报弹出窗口。

然而,在出现警报后,机器停止了大约一分钟。我不能按任何东西。

之后,我可以同时按下两个按钮,但什么也没有发生——console.log 和删除功能都没有。

我怎样才能解决这个问题?谢谢

import React from 'react';
import { StyleSheet, View, Text, TouchableOpacity, Alert } from 'react-native';
import CircleButton from '../elements/CircleButton';


  handlePress() {
const { currentUser } = firebase.auth();
const db = firebase.firestore();
db.collection(`users/${currentUser.uid}/friends`).doc(this.state.key).delete()
  .then(() => {
    console.log('Deleted');
    this.props.navigation.goBack();
  })
  .catch((error) => {
    console.log(error);
  });
  }

...

    <CircleButton
      style={styles.funcButton}
      onPress={()=>{
        Alert.alert(
          'Are you sure to delete?',
          'never recover',
          [
            {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
            {text: 'Delete', onPress: () => this.handlePress.bind(this), style: 'destructive'},
          ],
          { cancelable: false }
        )
      }}>
      {'\uf1f8'}
    </CircleButton>
4

1 回答 1

0

改变

{text: 'Delete', onPress: () => this.handlePress.bind(this), style: 'destructive'},

{text: 'Delete', onPress: this.handlePress, style: 'destructive'},

因为

箭头函数不绑定this

于 2018-05-13T13:44:39.693 回答