简体   繁体   English

使用 redux-thunk 处理错误,本机反应

[英]Error handling with redux-thunk, react native

I am using a redux setup in my react-native app using redux-thunk.我正在使用 redux-thunk 在我的本机应用程序中使用 redux 设置。 this is the simple login action这是简单的登录操作

export const doLogin=(request, onSuccess, onError)=>{
return dispatch=>{
    console.log(request)
    axios.post(`/AppUser/LoginValidation`, request).then(res=>{
        console.log(res.data)
        if(res.data.isSuccess=== true){
            dispatch({
                type:LOGIN,
                payload:{
                     auth_token: res.data.authToken,
                     responseResult: res.data.responseResult,
                     loggedIn: true
                }
            })
            onSuccess && onSuccess();
        }else{
            dispatch({
                type: ERROR,
                payload: res.data.message
            })
            onError && onError();
        }
    })
    .catch(err=>{
        console.log('err',err )
    })
}

} }

and this way i have used above code in front end:这样我在前端使用了上面的代码:

 props.doLogin(data,()=>{
        action.setErrors({password: props.errorMsg})
        // action.resetForm();
        // setMobileno(null)
        props.navigation.navigate('FLayout', { screen:'Home' })
    })

it redirects me if the login id and password are proper.如果登录 ID 和密码正确,它会重定向我。 if I put the invalid id and password, it still redirects me to the home page instead of showing me a message.如果我输入了无效的 ID 和密码,它仍然会将我重定向到主页,而不是向我显示消息。

It looks like you aren't passing the callback method to handle errors.看起来您没有传递回调方法来处理错误。 Try this:尝试这个:

const onSuccess = () => {
  props.navigation.navigate('FLayout', { screen:'Home' })
}

const onError = () => {
  action.setErrors({password: props.errorMsg})
  // action.resetForm();
  // setMobileno(null)
}

props.doLogin(data, onSuccess, onError)

I found a solution in another way, I have to write two callback in my front-end app section, like,我用另一种方式找到了解决方案,我必须在我的前端应用程序部分编写两个回调,例如,

props.doLogin(data,()=>{
    
    // action.resetForm();
    // setMobileno(null)
    props.navigation.navigate('FLayout', { screen:'Home' })
},
()=>{
    action.setErrors({password: props.errorMsg})
    console.log("another callback for error handle")
 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM