2017-11-14 9 views
0

AlertIOSウィンドウでユーザーが「はい」をクリックすると、定数を更新しようとしています。アクションがtrueの場合、OnPressから状態を更新します。

しかし、それが現在、

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は今、真である必要があり、その入力

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

誰でも上の任意の考えを持っていますこの行動師を働かせるには?

答えて

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 }, 
     ], 
    ) 
    } 
} 
2

ステートフルコンポーネントを使用して、executeRemovalの現在の状態を保存する必要があります。あなたたonPressイベントで

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

次の操作を行います。

onPress={() => this.setState({ executeRemoval: true })} 
関連する問題