简体   繁体   English

箭头中的隐式返回 function

[英]Implicit return in arrow function

I'm getting a weird warning that I'm Expected to return a value in arrow function in the following code我收到一个奇怪的警告,我Expected to return a value in arrow function

export const loginUser = userData => dispatch => {
    axios.get("http://localhost:5000/users")
        .then((res)=>{
            res.data.map(user => {                     <---- warning occurs here
                if(user.email === userData.email){
                    if(user.password === userData.password){
                        localStorage.setItem("user", JSON.stringify(user));
                        dispatch(setCurrentUser(user));
                    }
                }
            })
        })
        .catch((err)=>dispatch({
            type: GET_ERRORS,
            payload: err.data
        }))
}

But I thought it was not required to declare an explicit return in an arrow function, what am I doing wrong?但我认为不需要在箭头 function 中声明显式return ,我做错了什么? How can I fix this?我怎样才能解决这个问题?

For quick example when you are using map like this例如,当您像这样使用 map 时

const map1 = array1.map(x => x * 2);

you no need to use an explicit return您无需使用显式返回

but in scenario但在场景中

const map1 = array1.map(x => {
return x*2
});

the main difference is due to flower brackets which expect a return statement.主要区别在于花括号需要返回声明。

The code you provided is doing dispatch action in this case it doesn't require anything to return, I would suggest using foreach loop [UPDATED]您提供的代码正在执行调度操作,在这种情况下它不需要任何返回,我建议使用 foreach 循环[更新]

export const loginUser = userData => dispatch => {
    return axios.get("http://localhost:5000/users")
        .then((res)=>{
            res.data.foreach(user => {                     <---- warning occurs here
                if(user.email === userData.email){
                    if(user.password === userData.password){
                        localStorage.setItem("user", JSON.stringify(user));
                        dispatch(setCurrentUser(user));
                    }
                }
            })
        })
        .catch((err)=>dispatch({
            type: GET_ERRORS,
            payload: err.data
        }))
}

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

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