如果用户在AlertIOS窗口上按是,我试图将常量更新为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},
],
)
}
如果用户按下是,则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'})
]
})))
}
任何人都对如何让这个行为工作有任何想法?
答案 0 :(得分:2)
您需要使用有状态组件并将executeRemoval的当前状态保存在其中。
class yourClass extends Component {
constructor(props){
super(props);
this.state = {
executeRemoval = false
}
}
}
在您的onPress活动中,您执行以下操作:
onPress={() => this.setState({ executeRemoval: true })}
答案 1 :(得分:1)
创建一个简单的函数来处理你想要运行的动作。
示例强>
executeRemoval = () => {
const { id } = this.props;
removeDeck(id)
.then(() => this.props.deleteDeck(id))
.then(() => this.props.navigation.dispatch(NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Home'})
]
})))
}
deleteDeck = () => {
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: this.executeRemoval },
],
)
}
}