如果用户在 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/