How do I resolve eslint import/no-named-as-default

2019-04-19 03:37发布

After looking at the documentation for the import/no-named-as-default eslint rule, I'm still confused about what exactly I'm doing wrong.

I have the following file structure

.
├── ButtonBack.css
├── ButtonBack.jsx
├── __tests__
│   └── ButtonBack.test.jsx
└── index.js

The ButtonBack.jsx contains the following code

import React from 'react';
import PropTypes from 'prop-types';

export default class ButtonBack extends React.Component {
  ... code removed to keep example short ...
}

__tests__/ButtonBack.test.jsx contains the following code

import React from 'react';
import { shallow } from 'enzyme';
import ButtonBack from '../ButtonBack'; // <== this line has an eslint warning

... code removed to keep example short ...

The problem is, my linter says that import ButtonBack from '../ButtonBack violates the following lint rules:

I can't figure out why my import statement violates the lint rule. Removing the name of the class in ButtonBack.jsx (export default class extends React.Component) does not solve the issue either.

5条回答
萌系小妹纸
2楼-- · 2019-04-19 04:18

Ran into this same issue and from what I'm seeing you're going to just have to disable that rule (that's what I did at least)

"Unfortunately, React + Redux is the most common scenario. However, there are lots of other cases where HOCs will force developers to shut down this rule."

https://github.com/benmosher/eslint-plugin-import/issues/544

https://github.com/reactjs/react-redux/issues/119

https://github.com/18F/calc/pull/1235

.eslintrc

"rules": {
    "import/no-named-as-default": 0
}
查看更多
该账号已被封号
3楼-- · 2019-04-19 04:19

You can as well use an alias at export and the opposite alias on import

查看更多
霸刀☆藐视天下
4楼-- · 2019-04-19 04:22

Ran into this issue because I am using React+Redux:

export class ButtonBack extends React.Component { // code removed } export default connect(null, null)(ButtonBack);

So with the code above this will give me a warning:

import ButtonBack from ./ButtonBack;

changing to the following fixes the problem

import ConnectedButtonBack from ./ButtonBack;

Not exporting class ButtonBack would probably also fix the problem, but I like to export it to help with testing.

Source: http://redux.js.org/docs/recipes/WritingTests.html#connected-components

查看更多
叼着烟拽天下
5楼-- · 2019-04-19 04:24

This is rather silly for esLint but what I did to resolve it was to check file that is exporting, I accidentally had export class then export default connect. Still had lint error, re wrote import line in parent and eslint cleared warning.

查看更多
太酷不给撩
6楼-- · 2019-04-19 04:35

Original problem line :

import ButtonBack from '../ButtonBack'; 

Fixed Line:

import { ButtonBack } from '../ButtonBack'; 
查看更多
登录 后发表回答