[英]How to fix Eslint error: 'default' is restricted from being used as an exported name in my React Typescript project
I am updating eslint rules in my React project.我正在更新我的 React 项目中的 eslint 规则。
Currently I have this in the extend
property inside eslintrc.js
:目前我在
eslintrc.js
的extend
属性中有这个:
extends: [ 'airbnb', 'airbnb-typescript', 'airbnb/hooks', // "plugin:@typescript-eslint/recommended", // "plugin:@typescript-eslint/recommended-requiring-type-checking", // "plugin:eslint-comments/recommended", 'plugin:react/recommended', 'plugin:jest/recommended', 'plugin:prettier/recommended', ],
I am getting this error:我收到此错误:
error 'default' is restricted from being used as an exported name no-restricted-exports
错误“默认”被限制用作导出名称 no-restricted-exports
Our pattern for components is like this:我们的组件模式是这样的:
Button/ - Button.tsx - Button.spec.ts - Button.stories.tsx - index.ts
index.ts:索引.ts:
export { default } from './Button';从'./Button'导出{默认};
How to fix this?如何解决这个问题? Or do I have to override this eslint rule somehow?
还是我必须以某种方式覆盖这个 eslint 规则?
You'll need to disable this rule - no-restricted-exports
.您需要禁用此规则 -
no-restricted-exports
。 Your.eslintrc file will look like this你的.eslintrc 文件看起来像这样
{ "extends": [ 'airbnb', 'airbnb-typescript', 'airbnb/hooks', // "plugin:@typescript-eslint/recommended", // "plugin:@typescript-eslint/recommended-requiring-type-checking", // "plugin:eslint-comments/recommended", 'plugin:react/recommended', 'plugin:jest/recommended', 'plugin:prettier/recommended', ], "rules": { "no-restricted-exports": 0, } }
More documentation here - https://eslint.org/docs/latest/rules/no-restricted-exports .更多文档在这里 - https://eslint.org/docs/latest/rules/no-restricted-exports 。
Another option would be to not use default exports but instead do something like this:另一种选择是不使用默认导出,而是执行以下操作:
export const Button = () => {...};
and in the index.ts file:在 index.ts 文件中:
export * from './Button';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.