【问题标题】:[TYPESCRIPT]: react/require-default-props does not work correctly with my .eslintrc configuration[TYPESCRIPT]:react/require-default-props 在我的 .eslintrc 配置中无法正常工作
【发布时间】:2021-01-15 03:28:39
【问题描述】:

我正在用这种方式在 typescript 中设置默认道具

type Props = {
  message?: string,
  disableElevation?: boolean,
};

const BoxError = ({ message = 'Oops! Something went wrong!', disableElevation = false }: Props) => {

  return (
    <div>mybox error</div>
  )
}

但我总是收到错误react/require-default-props

使用的.eslintrc是这样的:

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": [
      "./tsconfig.json"
    ]
  },
  "plugins": [],
  "extends": [
    "airbnb-typescript",
    "airbnb/hooks",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking"
  ],
  "env": {
    "browser": true,
    "node": true,
    "jquery": true,
    "mocha": true
  },
  "globals": {
    "Routing": true
  },
  "rules": {
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn",
    "react-redux/connect-prefer-named-arguments": 0,
    "no-console": 2,
    "no-continue": "off",
    "no-undef": 0,
    "react/prefer-stateless-function": 2,
    "react/jsx-filename-extension": 0,
    "func-names": 0,
    "no-underscore-dangle": 0,
    "no-param-reassign": [ "error", { "props": false }],
    "semi": [2, "never"],
    "@typescript-eslint/semi": "off",
    "max-len": ["error", 200, 2, {
      "ignoreUrls": true,
      "ignoreComments": false,
      "ignoreRegExpLiterals": true,
      "ignoreStrings": true,
      "ignoreTemplateLiterals": true
    }],
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "js": "never",
        "jsx": "never",
        "ts": "never",
        "tsx": "never"
      }
    ],
    "curly":["error","all"],
    "jsx-a11y/click-events-have-key-events": 0,
    "jsx-a11y/no-noninteractive-element-interactions": 0,
    "jsx-a11y/label-has-for": 0,
    "react/jsx-closing-tag-location": 0
  },
  "settings": {
    "import/resolver": {
      "webpack": {
        "config": "webpack.common.js"
      }
    }
  },
}

我做错了什么?

【问题讨论】:

  • 我会定义为const BoxError: React.FC&lt;Props&gt; = ({ message = 'Oops! Something went wrong!', disableElevation = false }) =&gt; {... 有帮助吗?
  • 它可以工作,但在另一个组件中我得到了这个 ESLint: 'rows' is missing in props validation(react/prop-types)..显然 'rowsì 已定义

标签: reactjs typescript eslint eslint-config-airbnb


【解决方案1】:

尝试下一个示例:

type Props = {
  message?: string,
  disableElevation?: boolean,
};

const BoxError = ({ message = 'Oops! Something went wrong!', disableElevation = false }: Props) => {

  return (
    <div>mybox error</div>
  )
}

BoxError.defaultProps = {
  message: '',
  disableElevation: false,
}

【讨论】:

  • 我不喜欢复制默认道具
  • 也许你应该更新你的 react eslint 插件或提出问题
猜你喜欢
  • 2019-11-22
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多