在本机警报

时间:2018-09-07 07:17:50

标签: react-native alert

我正在将错误设置为true / false状态,如果error = true,则要显示警报
所以我做到了。

constructor() {
    super()
    this.state = {
        email: "",
        password: "",
        error: false
    }
    this.handleSignUp = this.handleSignUp.bind(this)
}

和功能

handleSignUp() {
    fetch('http://myIp:9999/signUp', {
        method: 'POST',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(this.state),
    })
        .then((res) => {
            console.log("here")
            console.log(res)
            this.setState({error: false})
        }).catch((e) => {
        if (e) {
            console.log('e')
            console.log(e.status)
            this.setState({error: true})
        }
    })

}

并将渲染方法设置为

 render() {

    var alert1 = alert(
        "User exist",
        "User with same Email exist. Try some to login or use other Email",
        [
            {text: "Ok", style: 'cancle', onPress: () => this.setState({error: false})}
        ]
    )


    return (
        <View style={styles.container}>
            {this.state.error ? alert1 : ""}
            <Header function1={() => this.props.navigation.openDrawer()}/>
            ....
        </View>
    )
}

当我打开此屏幕时,无论错误是true还是false,结果都是无关紧要的

所以我更改了alert1。

var alert1 =    (<View> 
                                    alert(
                                            "User exist",
                                            "User with same Email exist. Try some to login or use other Email",
                                            [
                                                {text:"Ok",style:'cancle',onPress:()=> this.setState({error:false})}
                                            ]
                                        )
                                    </View>)

现在错误已解决,但屏幕上显示了负载警报。

1 个答案:

答案 0 :(得分:1)

np.object