【问题标题】:ESLint: Promise-returning function provided to attribute where a void return was expectedESLint:提供给属性的承诺返回函数,其中预期返回无效
【发布时间】:2023-02-03 05:55:00
【问题描述】:

我正在使用react-hook-form

const onSubmit = async (values: IProductType) => {
  const response = await dispatch(createProductType({
    productType: values,
    shopId: shopId as string
  }));
  const newCreatedProductType = response.payload as IProductType;

  if (response.meta.requestStatus === requestStatusSuccess) {
    dispatch(getMessageAction(t('createSuccess', { ns: 'productType', name: values.name }), 'success'));
    dispatch(addProductType(newCreatedProductType));

    if (onCreateSuccess) {
        onCreateSuccess(newCreatedProductType);
    }

    reset();
}

};

并将其传递给<form>

//Promise-returning function provided to attribute where a void return was expected
<form onSubmit={handleSubmit(onSubmit)}>

我尝试将它包装在一个空函数中并修复了错误,但随后提交停止工作:

<form onSubmit={() => { handleSubmit(onSubmit) }}>

【问题讨论】:

  • 你找到修复了吗?

标签: reactjs eslint react-hook-form


【解决方案1】:

将此规则添加到您的.eslintrc.json

"rules": {
  "@typescript-eslint/no-misused-promises": [2, {
    "checksVoidReturn": {
      "attributes": false
    }
  }]
},

有同样的问题,并在 react-hook-form github 讨论中发现 this answer 很有帮助。链接中有关它的更多信息

【讨论】:

    猜你喜欢
    • 2022-12-11
    • 2019-09-15
    • 1970-01-01
    • 2020-12-23
    • 2019-06-09
    • 2017-04-05
    • 2017-04-02
    • 1970-01-01
    相关资源
    最近更新 更多