如何以编程方式删除警报提示?

时间:2017-06-22 20:37:18

标签: javascript reactjs react-native react-native-android react-native-ios

点击按钮,我启动一个需要一段时间的过程,一旦完成,我会导航到指定的屏幕。

正在处理中,我Alert根据docs提示“ loading ”提示:“默认情况下,唯一的按钮就是'确定' '按钮'。一旦完成该过程,我再次警告数据已准备就绪。

问题是我得到两个相互提示,并且他们需要删除用户的点击。

我想在显示第二个提示之前删除第一个提示(并且可能为第二个设置计时器然后将其删除)。

如何以编程方式删除<?php require_once('iSDK/isdk.php'); $appname = "xxxxx"; $apikey ="xxxxxxxxxxxxxxxxx"; set_time_limit(0); ob_implicit_flush(true); $app = new iSDK; $contactId = intval($_GET['contactId']); if(($appname == NULL) || ($apikey == NULL)){ echo "<b>Error</b> - make sure you fill out all the fields"; exit(0); } if(!$app->cfgCon('i', $appname, $apikey)) { echo "No connected"; exit(); } $result = $app->runAS($contactId, '123'); print_r($result); echo "Contact Id:". $contactId; ?> 提示?

1 个答案:

答案 0 :(得分:1)

为了实现您的要求,基本的解决方法是创建一个单独的组件来代替警报。

我写的组件接受了两个道具。 textvisible

在屏幕内,你可以这样添加:

import React from 'react'
[....]

export default class Screen extends React.Component {
  state = {
    visible: true,
    text: "Loading... Please wait"
  }

  drawAlert() {
    setTimeout(() => {
      this.setState({text: "Will dismiss in 1 second"}, () => {
        setTimeout(() => {
          this.setState({visible: false})
        }, 1000);
      })
    }, 4000); // fake API request
    return (
      <Alert text={this.state.text} visible={this.state.visible} />
    )
  }

  render() {
    return(
      <Alert text={this.state.text} visible={this.state.visible} />
    )
  }
}

这是alert组件

import React, { Component } from 'react'
import { View, TouchableOpacity, Text } from 'react-native'

export default class Alert extends Component {
  state = {
    text: this.props.text,
    visible: this.props.visible
  }

  componentWillReceiveProps(nextProps) {
    this.setState({text: nextProps.text, visible: nextProps.visible})
  }

  drawBox() {
    if (this.state.visible) {
      return(
        <View style={styles.container}>
          <View style={styles.boxContainer}>
            <View style={styles.textContainer}>
              <Text style={styles.text}>{this.state.text}</Text>
            </View>
            <TouchableOpacity onPress={this.props.onPress} style={styles.buttonContainer}>
              <Text style={styles.buttonText}>OK</Text>
            </TouchableOpacity>
          </View>
        </View>
      )
    }
  }

  render() {
    return(
      <View style={styles.container}>
        {this.drawBox()}
      </View>
    )
  }
}

const styles = {
  wrapper: {
    flex: 1,
  },
  container: {
    zIndex: 99999,
    position: "absolute",
    backgroundColor: "rgba(0, 0, 0, 0.1)",
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    justifyContent: 'center',
    alignItems: 'center',
  },
  boxContainer: {
      backgroundColor: "#FFF",
      borderRadius: 2,
      padding: 10,
      width: 300,
      borderColor: "rgba(0,0,0,.1)",
      borderBottomWidth: 0,
      shadowOffset: { width: 0, height: 2 },
      elevation: 1,
      padding: 20
  },
  textContainer: {
    marginBottom: 20
  },
  text: {
    color: "#424242",
    fontFamily: "Roboto",
    fontSize: 18
  },
  buttonContainer: {
    alignItems: 'flex-start'
  },
  buttonText: {
    color: "#009688"
  }
}

希望它有所帮助。