【问题标题】:React Native Alert crash(error) Delete conformationReact Native Alert crash(error) 删除构象
【发布时间】:2018-05-13 12:21:18
【问题描述】:

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

这是我的代码。 现在这显示了带有标题、说明和两个按钮的警报弹出窗口,正如我所期望的那样。

但是,在出现警报后,机器停止了大约一分钟。 我什么都不能按。

之后,我可以同时按下两个按钮,但什么也没有发生——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>

【问题讨论】:

  • onPress: () =&gt; this.handlePress.bind(this) 应该是 onPress: () =&gt; this.handlePress() 或只是 onPress: this.handlePress.bind(this)
  • 感谢您的评论!但对我不起作用。

标签: react-native alert onpress


【解决方案1】:

改变

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

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

因为

arrow functions don’t bind this.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多