Javascript 如果操作为真,则从OnPress更新状态
如果用户在AlertIOS窗口中按Yes,我会尝试将常数更新为true 但目前无法使其按其构建方式运行Javascript 如果操作为真,则从OnPress更新状态,javascript,reactjs,react-native,Javascript,Reactjs,React Native,如果用户在AlertIOS窗口中按Yes,我会尝试将常数更新为true 但目前无法使其按其构建方式运行 deleteDeck = () => { const {id} = this.props const executeRemoval = false if (Platform.OS === 'ios') { AlertIOS.alert( 'Remove Deck', 'Are you sure you want to d
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},
],
)
}
如果用户按Yes,const executemoval现在应该为true,并且通过输入
{executeRemoval &&
removeDeck(id)
.then(() => this.props.deleteDeck(id))
.then(() => this.props.navigation.dispatch(NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Home'})
]
})))
}
有人对如何让这个behvior工作有什么想法吗 您需要使用有状态组件并将ExecuteMoval的当前状态保存在其中
class yourClass extends Component {
constructor(props){
super(props);
this.state = {
executeRemoval = false
}
}
}
在onPress事件中,您可以执行以下操作:
onPress={() => this.setState({ executeRemoval: true })}
您需要使用有状态组件并在其中保存ExecuteMoval的当前状态
class yourClass extends Component {
constructor(props){
super(props);
this.state = {
executeRemoval = false
}
}
}
在onPress事件中,您可以执行以下操作:
onPress={() => this.setState({ executeRemoval: true })}
创建一个简单的函数,该函数将处理要运行的操作 示例
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 },
],
)
}
}
创建一个简单的函数,该函数将处理要运行的操作 示例
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 },
],
)
}
}