react-demo:项目使用ESLint和prettier(不使用create-react-app脚手架)

react项目使用eslint


npm install eslint --save-dev

初始化配置文件

# 初始化eslintrc文件
npx eslint --init
√ How would you like to use ESLint? · style       
√ What type of modules does your project use? · esm
√ Which framework does your project use? · react
√ Does your project use TypeScript? · No / Yes
√ Where does your code run? · browser, node
√ How would you like to define a style for your project? · guide
√ Which style guide do you want to follow? · airbnb      
√ What format do you want your config file to be in? · JavaScript
Checking peerDependencies of eslint-config-airbnb@latest
The config that you've selected requires the following dependencies:

eslint-plugin-react@^7.28.0 eslint-config-airbnb@latest eslint@^7.32.0 || ^8.2.0 eslint-plugin-import@^2.25.3 eslint-plugin-jsx-a11y@^6.5.1 eslint-plugin-react-hooks@^4.3.0
√ Would you like to install them now? · No / Yes
√ Which package manager do you want to use? · npm
Installing eslint-plugin-react@^7.28.0, eslint-config-airbnb@latest, eslint@^7.32.0 || ^8.2.0, eslint-plugin-import@^2.25.3, eslint-plugin-jsx-a11y@^6.5.1, eslint-plugin-react-hooks@^4.3.0


一些问题

  • Missing an explicit type attribute for button

详见:https://github.com/jsx-eslint/eslint-plugin-react/blob/HEAD/docs/rules/button-has-type.md

//给button添加type属性
<button type="button" className="btn" onClick={() => go('/home')}>Home </button>
<button type="button" className="btn" onClick={() => go('/looking')}>Looking </button>
<button type="button" className="btn" onClick={() => go('/about')}>ablout</button>
  • Unexpected use of file extension “jsx” for "./pages/Looking/index.jsx"eslintimport/extensions
    详细见:https://github.com/import-js/eslint-plugin-import/blob/v2.26.0/docs/rules/extensions.md

在eslint配置文件中进行配置

  rules: {
    'import/extensions': ['error', 'always', { ignorePackage: false }],
  },

遇到问题可以查看相关插件的使用规则

    "eslint": "^8.28.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-jsx-a11y": "^6.6.1",
    "eslint-plugin-react": "^7.31.11",
    "eslint-plugin-react-hooks": "^4.6.0",

react项目使用prettier

详细见文档:https://prettier.io/docs/en/install.html

安装依赖

npm install --save-dev --save-exact prettier

新建文件.prettierignore

忽略这里的文件,不进行preitter校验

# Ignore artifacts:
node_modules
build
dist


# Ignore all HTML files:
*.html

# Ignore all MD files:
*.md

新建文件.prettierrc.js or .prettierrc.json

配置prettier规则

//.prettierrc.js
module.exports = {
  singleQuote: true,
};

//.prettierrc.json
{
  "singleQuote": true,
}

检查文件

npx prettier --write .

npx prettier --check .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值