小编Ale*_*rto的帖子

与 TypeScript、React、Eslint 和简单的 Arrow Functions 组件作斗争

我整个上午都在为这个问题苦苦挣扎,但在任何地方都找不到解决方案。我是打字稿的新手,我正在尝试使用 Eslint 和 Prettier 正确设置它,以确保代码格式正确。

所以,我在创建功能组件时面临的问题。根据备忘单,我正在尝试导出一个简单的组件,例如:

import React from "react";

type DivProps = {
  text: string;
};

const Div = ({ text }: DivProps): JSX.Element => (<div>{text}</div>)

export default Div;
Run Code Online (Sandbox Code Playgroud)

然而,eslint 抱怨说“函数组件不是函数表达式”。

运行修复时,它会将我的函数转换为未命名函数:

const Div = function ({ text }: DivProps): JSX.Element {
  return <div>{text}</div>;
};
Run Code Online (Sandbox Code Playgroud)

然后它会抱怨说“意外的未命名函数”。

即使我尝试将函数重构为:

function Div({ text }: DivProps): JSX.Element {
  return <div>{text}</div>;
};
Run Code Online (Sandbox Code Playgroud)

我仍然收到同样的错误,说“函数组件不是函数表达式”。

我的 .eslintrc.js 文件是:

module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: ["airbnb", "plugin:@typescript-eslint/recommended", "prettier"],
  parser: "@typescript-eslint/parser",
  parserOptions: { …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs eslint eslint-config-airbnb eslintrc

50
推荐指数
3
解决办法
5万
查看次数