javascript - 如果操作为 true,则从 OnPress 更新状态

标签 javascript reactjs react-native

如果用户在 AlertIOS 窗口上按"is",我会尝试将常量更新为 true。

但目前无法使其正常工作

deleteDeck = () => {
    const {id} = this.props
    const executeRemoval = false
    if (Platform.OS === 'ios') {
      AlertIOS.alert(
        'Remove Deck',
        'Are you sure you want to delete this deck?',
        [
          {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
          {text: 'Yes', onPress: () =>  executeRemoval = true},
        ],
      )
    }

如果用户按"is",const executeRemoval 现在应该为 true,然后输入

{executeRemoval &&
      removeDeck(id)
      .then(() =>  this.props.deleteDeck(id))
      .then(() =>  this.props.navigation.dispatch(NavigationActions.reset({
          index: 0,
          actions: [
            NavigationActions.navigate({ routeName: 'Home'})
          ]
        })))
      }

有人对如何让这种行为发挥作用有什么想法吗?

最佳答案

您需要使用有状态组件并将executeRemoval的当前状态保存在其中。

class yourClass extends Component {
    constructor(props){
        super(props);
        this.state = {
            executeRemoval = false
        }
    }
}

在 onPress 事件中执行以下操作:

onPress={() => this.setState({ executeRemoval: true })}

关于javascript - 如果操作为 true,则从 OnPress 更新状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47288652/

相关文章:

Android 无法完成 session : -26 new target SDK 22 doesn't support runtime permissions but the old target SDK 23 does

javascript - JQuery 不适用于创建后的元素

javascript - 为什么在回调中定义的变量可以在代码中的其他位置访问?

javascript - 三人组 : assign different colors to each vertex in a geometry

reactjs - 如果 item.image 为 null,则使用占位符图像(React Native)

android - 如何处理 React Native 底部选项卡中的后退按钮行为?

javascript - react native : How to set background color for the whole app and individual scene in react-native-router-flux?

javascript - then() 函数是否返回反射(reflect)先前 promise 结果的 promise ?

javascript - 为什么react js中没有定义router?

javascript - React JS 自定义图表