javascript - 箭头函数中的隐式返回
问题描述
我收到一个奇怪的警告,我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
}))
}
但我认为不需要return
在箭头函数中声明一个显式,我做错了什么?我怎样才能解决这个问题?
解决方案
例如,当您使用这样的地图时
const map1 = array1.map(x => x * 2);
您无需使用显式返回
但在场景中
const map1 = array1.map(x => {
return x*2
});
主要区别在于花括号需要返回声明。
您提供的代码正在执行调度操作,在这种情况下它不需要任何返回,我建议使用 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
}))
}
推荐阅读
- gtk - GtkOverlay:防止重绘背景小部件?
- c# - 如何将网格停靠在顶部?
- typescript - VSCode - TSLint 错误 ENOTDIR 无效 cwd
- java - Java JPA 与类的 Hibernate 映射
- reactjs - 无法加载资源错误:net::ERR_CONNECTION_REFUSED
- ios - 视图元素未显示在 ViewController 中
- ios - 用 safeAreaInsets 或 LayoutGuide **WITHOUT** interface builder 替换 edgesForExtendedLayout
- javascript - 我可以在 Qualtrics 调查中使用 javascript 捕获验证错误吗?
- c# - 使用 LINQ 将整数列表映射到 MinMax 范围列表
- c++ - 这个 FFT 平滑器如何在 c++/openframeworks 中工作?