Nimrod
Nimrod

Reputation: 389

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

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? How can I fix this?

Upvotes: 0

Views: 511

Answers (1)

akhil
akhil

Reputation: 1883

For quick example when you are using map like this

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]

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
        }))
}

Upvotes: 3

Related Questions