【问题标题】:Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>` arrow-body-style [duplicate]箭头主体周围出现意外的块语句;在 `=>` 箭头体样式之后立即移动返回值 [重复]
【发布时间】:2021-12-26 22:12:56
【问题描述】:
const loadUsers = () => {
  return function (dispatch) {
    axios
      .get(`${process.env.REACT_APP_API}`)
      .then((resp) => {
        console.log(resp);
        dispatch(getUsers(resp.data));
      })
      .catch((error) => console.log(error));
  };
};

我在上面的代码中遇到了这个错误。

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

如果箭头函数的块只有一个返回语句,您可以省略该块和return 关键字。根据您使用的 linter,您确实需要这样做:

const loadUsers = () => function (dispatch) {
  axios
    .get(`${process.env.REACT_APP_API}`)
    .then((resp) => {
      console.log(resp);
      dispatch(getUsers(resp.data));
    })
    .catch((error) => console.log(error));
};

【讨论】:

  • 是的,它的工作,但得到另一个错误
  • 第 10:3 行:在此表达式implicit-arrow-linebreak 之前不应有换行符
  • @Omkar 显然function 不能换行。已编辑和修复。
猜你喜欢
  • 2021-10-19
  • 2019-03-09
  • 1970-01-01
  • 2019-09-29
  • 2018-04-10
  • 1970-01-01
  • 2020-10-15
相关资源
最近更新 更多