简体   繁体   English

如何修复 Eslint 错误:在我的 React Typescript 项目中,“默认”被限制用作导出名称

[英]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.jsextend属性中有这个:

 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.

相关问题 React typescript eslint 错误解析错误:意外的令牌? 如何解决这个问题? - React typescript eslint error Parsing error: Unexpected token ! How to fix this? 如何修复我的反应应用程序中的 eslint 错误“未定义 pendo” - how to fix eslint error 'pendo is not defined' in my react application ESLint: '@material-ui/core/Button' 导入被限制使用 - ESLint: '@material-ui/core/Button' import is restricted from being used 如何修复 eslint 在反应中的“组件已定义但从未使用”? - How to fix "Component is defined but never used" for eslint in react? 尝试使用 React + TypeScript 项目修复 tslint 错误 - Trying to fix tslint error with React + TypeScript project 如何使用 ESLint 检查 React/Typescript 项目中的 prop 错误? - How do you check for prop errors in a React/Typescript project with ESLint? 如何让 `eslint-plugin-react-hooks` 对导出为 `default` 的功能组件进行 lint? - How can I get `eslint-plugin-react-hooks` to lint functional components that are exported as `default`? 如何修复错误:尝试导入错误:“Route”未从“react-router-dom”导出 - How to fix error: Attempted import error: 'Route' is not exported from 'react-router-dom' 使用ESlint整理React + Typescript项目 - Linting React+Typescript project with ESlint 如何修复错误:尝试导入错误:“Navlink”未从“react-router-dom”导出 - How to fix the error: Attempted import error: 'Navlink' is not exported from 'react-router-dom'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM