gpt4 book ai didi

reactjs - Eslint 与 React CSS 模块

转载 作者:行者123 更新时间:2023-12-03 13:37:17 25 4
gpt4 key购买 nike

我正在尝试将 React CSS 模块添加到我的项目中,但我遇到了 eslint 问题。我的登录 View 如下所示:

import React, { PropTypes as T } from 'react';
import {ButtonToolbar, Button} from 'react-bootstrap';
import Messages from '../Messages/Messages';
import AuthService from '../../utils/AuthService';
import CSSModules from 'react-css-modules';
import styles from './styles.module.css';

class Login extends React.Component {

render() {

let rootStyle = {
textAlign: 'center'
};

let toolbarStyle = {
display: 'inline-block'
};

const { auth } = this.props;

return (
<div styleName="root">
<h2>Login</h2>
<Messages auth={this.props.auth}></Messages>
<ButtonToolbar styleName="toolbar">
<Button bsStyle="primary" onClick={auth.login.bind(this)}>Login</Button>
</ButtonToolbar>
</div>
);
}
}

Login.PropTypes = {
location: T.object,
auth: T.instanceOf(AuthService)
};

export default CSSModules(Login, styles);

这是我的原始 .eslintrc 文件,在添加导入/忽略设置之前:

{
"extends": [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings"
],
"plugins": [
"react"
],
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true,
"experimentalObjectRestSpread": true
}
},
"env": {
"es6": true,
"browser": true,
"node": true,
"jquery": true,
"mocha": true
},
"rules": {
"quotes": 0,
"no-console": 1,
"no-debugger": 1,
"no-var": 1,
"semi": [1, "always"],
"no-trailing-spaces": 0,
"eol-last": 0,
"no-unused-vars": 0,
"no-underscore-dangle": 0,
"no-alert": 0,
"no-lone-blocks": 0,
"jsx-quotes": 1,
"react/display-name": [ 1, {"ignoreTranspilerName": false }],
"react/forbid-prop-types": [1, {"forbid": ["any"]}],
"react/jsx-boolean-value": 1,
"react/jsx-closing-bracket-location": 0,
"react/jsx-indent-props": 0,
"react/jsx-key": 1,
"react/jsx-max-props-per-line": 0,
"react/jsx-no-bind": 1,
"react/jsx-no-duplicate-props": 1,
"react/jsx-no-literals": 0,
"react/jsx-no-undef": 1,
"react/jsx-pascal-case": 1,
"react/jsx-sort-prop-types": 0,
"react/jsx-sort-props": 0,
"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 1,
"react/no-danger": 1,
"react/no-did-mount-set-state": 1,
"react/no-did-update-set-state": 1,
"react/no-direct-mutation-state": 1,
"react/no-multi-comp": 1,
"react/no-set-state": 0,
"react/no-unknown-property": 1,
"react/prefer-es6-class": 1,
"react/prop-types": 1,
"react/react-in-jsx-scope": 1,
"react/require-extension": 1,
"react/self-closing-comp": 1,
"react/sort-comp": 1,
"react/wrap-multilines": 1
}
}

通过 eslint 运行它会出现 2 个错误:

6:20  error    Parse errors in imported module './styles.module.css': Unexpected token . (1:1)  import/namespace
6:20 error Parse errors in imported module './styles.module.css': Unexpected token . (1:1) import/default

当我将以下设置添加到 .eslintrc 文件时:

   "settings": {
"import/ignore": [".css$"]
}

解析错误消失,但出现以下错误:

1:8   error    No default export found in module      import/default
1:17 error PropTypes not found in 'react' import/named
5:8 error No default export found in module import/default

关于如何让 eslint 与 React CSS 模块完美配合,有什么建议吗?

最佳答案

我确实找到了一个解决方案:如果我将 .eslintrc 中的导入/忽略设置更改为

    "settings": {
"import/ignore": [".css$","node_modules/*"]
}

然后所有的 linting 错误都会消失。我想避免对 node_modules 中的所有内容进行 linting 是可以的,但它让我烦恼,我不知道是什么导致了错误。

关于reactjs - Eslint 与 React CSS 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39939067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com