React Native - setState doesn't work in promise statement

时间:2018-02-26 17:42:12

标签: react-native

I'm new to react native and I've been trying to create a login page for an app using a modal. There is a problem, however, whenever I try to remove the modal by using

this.setState({modalVisable: false})

Inside a .then statement, nothing happens!

When I place the setState inside the .catch statement, it works and the modal disappears. Why won't it work inside .then?

Here's the code:

    loginSubmitted(){
    Turbo({site_id: config.TURBO_APP_ID}).fetch('user', {username: this.state.login.username})
    .then((data=>{
        if(data.length==0){
            return Turbo({site_id: config.TURBO_APP_ID}).createUser(this.state.login)
            }else{
            return Turbo({site_id: config.TURBO_APP_ID}).login(this.state.login)
            }
    }))
    .then((data)=>{
        this.setState({modalVisible: false})
    })
    .catch((err)=>{
        alert(err.message)
        this.setState({modalVisible: false})
    })
}

0 个答案:

没有答案