最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React項(xiàng)目配置prettier和eslint的方法

 更新時(shí)間:2022年06月27日 10:04:28   作者:一個(gè)爬坑的Coder  
這篇文章主要介紹了React項(xiàng)目配置prettier和eslint的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

參考視頻: https://www.bilibili.com/video/BV1rh411e7E5?vd_source=eee62ea3954ac01bff9e87e2a7b40084

  • prettier代碼格式化
  • eslint js語(yǔ)法檢查
  • stylellint css樣式檢查

配置prettier和eslint

1.初始化React項(xiàng)目

npx create-react-app study_react

2.安裝vscode插件 prettiereslint, 配置vscode

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

3.安裝相關(guān)依賴

yarn add -D prettier eslint

在代碼格式化時(shí)采用Perttier規(guī)則,而我們代碼校驗(yàn)使用的是ESLint,如果同一個(gè)規(guī)則配置不一致,往往就會(huì)出現(xiàn)沖突問(wèn)題;
比如:字符串單、雙引號(hào)的配置,eslint后把字符串變成單引號(hào),更新文件代碼過(guò)后,重新保存(Prettier)又自動(dòng)格式化后變成雙引號(hào),導(dǎo)致代碼校驗(yàn)異常。

解決方案1: 兩者配置文件部分配置修改成一致.
解決方案2: 安裝相關(guān)插件(Prettier 和 ESLint 沖突解決方案 eslint-config-prettier eslint-plugin-prettier)

eslint-config-prettier 禁用 eslint 沖突配置
eslint-plugin-prettier Prettier先格式化 (默認(rèn)是先eslint格式化,再Prettier格式化)
yarn add -D eslint-config-prettier eslint-plugin-prettier

4.優(yōu)雅的提示錯(cuò)誤

“extends”: [“eslint:recommended”, “plugin:react/recommended”], 默認(rèn)是eslint:recommended,(步驟6后面會(huì)看到這個(gè)配置)
https://www.npmjs.com/package/eslint-config-airbnb

npx install-peerdeps --dev eslint-config-airbnb

5.初始化.eslintrc.json文件

npx eslint --init

如果全局安裝了eslint (npm install -g eslint )了, 可以直接使用eslint --init

根據(jù)提示勾選:

在這里插入圖片描述

安裝完成的eslintrc.json文件

{
 
  "env": {
 
    "browser": true,
    "es2021": true
  },
  "extends": ["eslint:recommended", "plugin:react/recommended"],
  "parserOptions": {
 
    "ecmaFeatures": {
 
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": ["react"],
  "rules": {
 
    "indent": ["error", "tab"],
    "linebreak-style": ["error", "windows"],
    "quotes": ["error", "double"],
    "semi": ["error", "always"]
  }
}

6.修改eslintrc.json優(yōu)雅提示(也就是步驟4所提到)

"extends": ["airbnb", "prettier", "plugin:react/recommended"],
  "plugins": ["prettier", "react"],

7.新建.prettierrc文件

更多規(guī)則: https://www.prettier.cn/docs/options.html

{
 
  "singleQuote": false,
  "endOfLine": "lf"
}

8.可自行定義eslintrc.json規(guī)則

https://eslint.bootcss.com/docs/rules/

9.效果

在這里插入圖片描述

10.讓提示更細(xì)致

eslintrc.json追加rules, "prettier/prettier": "error",

在這里插入圖片描述

最終的兩個(gè)文件的配置

// eslint
{
 
  "env": {
 
    "browser": true,
    "es2021": true
  },
  "extends": ["airbnb", "prettier", "plugin:react/recommended"],
  "parserOptions": {
 
    "ecmaFeatures": {
 
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": ["prettier", "react"],
  "rules": {
 
    "prettier/prettier": "error",
    "indent": ["off", "tab"],
    "linebreak-style": ["off", "windows"],
    "quotes": ["error", "double"],
    "semi": ["error", "always"]
  }
}
===============================================================================
// prettier
{
 
  "singleQuote": false,
  "endOfLine": "lf"
}

配置stylelint

1.安裝相關(guān)依賴

yarn add stylelint stylelint-config-stardand stylelint-config-prettier stylelint-order -D
- stylelint-config-prettier解決和prettier沖突
- stylelint-order 排序css屬性

2.新建.stylelint.json文件

stylelint相關(guān)規(guī)則: http://stylelint.docschina.org/user-guide/rules/

{
 
  "plugins": ["stylelint-order"],
  "extends": ["stylelint-config-standard", "stylelint-config-prettier"],
  "rules": {
 
    "property-no-unknown": true,
    "comment-no-empty": [
      true,
      {
 
        "message": "禁止空注釋"
      }
    ],
    "order/properties-order": [
      "position",
      "top",
      "right",
      "bottom",
      "left",
      "z-index",
      "display",
      "float",
      "width",
      "height",
      "max-width",
      "max-height",
      "min-width",
      "min-height",
      "padding",
      "padding-top",
      "padding-right",
      "padding-bottom",
      "padding-left",
      "margin",
      "margin-top",
      "margin-right",
      "margin-bottom",
      "margin-left",
      "margin-collapse",
      "margin-top-collapse",
      "margin-right-collapse",
      "margin-bottom-collapse",
      "margin-left-collapse",
      "overflow",
      "overflow-x",
      "overflow-y",
      "clip",
      "clear",
      "font",
      "font-family",
      "font-size",
      "font-smoothing",
      "osx-font-smoothing",
      "font-style",
      "font-weight",
      "hyphens",
      "src",
      "line-height",
      "letter-spacing",
      "word-spacing",
      "color",
      "text-align",
      "text-decoration",
      "text-indent",
      "text-overflow",
      "text-rendering",
      "text-size-adjust",
      "text-shadow",
      "text-transform",
      "word-break",
      "word-wrap",
      "white-space",
      "vertical-align",
      "list-style",
      "list-style-type",
      "list-style-position",
      "list-style-image",
      "pointer-events",
      "cursor",
      "background",
      "background-attachment",
      "background-color",
      "background-image",
      "background-position",
      "background-repeat",
      "background-size",
      "border",
      "border-collapse",
      "border-top",
      "border-right",
      "border-bottom",
      "border-left",
      "border-color",
      "border-image",
      "border-top-color",
      "border-right-color",
      "border-bottom-color",
      "border-left-color",
      "border-spacing",
      "border-style",
      "border-top-style",
      "border-right-style",
      "border-bottom-style",
      "border-left-style",
      "border-width",
      "border-top-width",
      "border-right-width",
      "border-bottom-width",
      "border-left-width",
      "border-radius",
      "border-top-right-radius",
      "border-bottom-right-radius",
      "border-bottom-left-radius",
      "border-top-left-radius",
      "border-radius-topright",
      "border-radius-bottomright",
      "border-radius-bottomleft",
      "border-radius-topleft",
      "content",
      "quotes",
      "outline",
      "outline-offset",
      "opacity",
      "filter",
      "visibility",
      "size",
      "zoom",
      "transform",
      "box-align",
      "box-flex",
      "box-orient",
      "box-pack",
      "box-shadow",
      "box-sizing",
      "table-layout",
      "animation",
      "animation-delay",
      "animation-duration",
      "animation-iteration-count",
      "animation-name",
      "animation-play-state",
      "animation-timing-function",
      "animation-fill-mode",
      "transition",
      "transition-delay",
      "transition-duration",
      "transition-property",
      "transition-timing-function",
      "background-clip",
      "backface-visibility",
      "resize",
      "appearance",
      "user-select",
      "interpolation-mode",
      "direction",
      "marks",
      "page",
      "set-link-source",
      "unicode-bidi",
      "speak"
    ]
  }
}

3.效果

在這里插入圖片描述

保存自動(dòng)修復(fù)

采用的vscode編輯器, 往setting.json添加配置

"editor.codeActionsOnSave": {
 
	"source.fixAll.stylelint": true,
	"source.fixAll.eslint": true
},

到此這篇關(guān)于React項(xiàng)目配置prettier和eslint的文章就介紹到這了,更多相關(guān)React配置prettier和eslint內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一文帶你了解React中的并發(fā)機(jī)制

    一文帶你了解React中的并發(fā)機(jī)制

    React 18.2.0 引入了一系列并發(fā)機(jī)制的新特性,旨在幫助各位開(kāi)發(fā)者更好地控制和優(yōu)化應(yīng)用程序的性能和用戶體驗(yàn),下面我們就來(lái)看看如何利用這些新特性構(gòu)建更高效、更響應(yīng)式的應(yīng)用程序吧
    2024-03-03
  • react-native ListView下拉刷新上拉加載實(shí)現(xiàn)代碼

    react-native ListView下拉刷新上拉加載實(shí)現(xiàn)代碼

    本篇文章主要介紹了react-native ListView下拉刷新上拉加載實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • react中useState改變值不渲染的解決方式

    react中useState改變值不渲染的解決方式

    這篇文章主要介紹了react中useState改變值不渲染的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • React中setState/useState的使用方法詳細(xì)介紹

    React中setState/useState的使用方法詳細(xì)介紹

    這篇文章主要介紹了React中setState/useState的使用方法,useState 和 setState 在React開(kāi)發(fā)過(guò)程中 使用很頻繁,但很多人都停留在簡(jiǎn)單的使用階段,并沒(méi)有正在了解它們的執(zhí)行機(jī)制
    2023-04-04
  • React狀態(tài)管理Redux的使用介紹詳解

    React狀態(tài)管理Redux的使用介紹詳解

    redux是redux官方react綁定庫(kù),能夠使react組件從redux store中讀取數(shù)據(jù),并且向store分發(fā)actions以此來(lái)更新數(shù)據(jù),這篇文章主要介紹了react-redux的設(shè)置,需要的朋友可以參考下
    2022-09-09
  • 淺析react里面如何封裝一個(gè)通用的Ellipsis組件

    淺析react里面如何封裝一個(gè)通用的Ellipsis組件

    這篇文章主要為大家詳細(xì)介紹了在react里面如何封裝一個(gè)通用的Ellipsis組件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • react antd checkbox實(shí)現(xiàn)全選、多選功能

    react antd checkbox實(shí)現(xiàn)全選、多選功能

    目前好像只有table組件有實(shí)現(xiàn)表格數(shù)據(jù)的全選功能,如果說(shuō)對(duì)于list,card,collapse等其他組件來(lái)說(shuō),需要自己結(jié)合checkbox來(lái)手動(dòng)實(shí)現(xiàn)全選功能,這篇文章主要介紹了react antd checkbox實(shí)現(xiàn)全選、多選功能,需要的朋友可以參考下
    2024-07-07
  • react實(shí)現(xiàn)移動(dòng)端下拉菜單的示例代碼

    react實(shí)現(xiàn)移動(dòng)端下拉菜單的示例代碼

    這篇文章主要介紹了react實(shí)現(xiàn)移動(dòng)端下拉菜單的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • react腳手架構(gòu)建運(yùn)行時(shí)報(bào)錯(cuò)問(wèn)題及解決

    react腳手架構(gòu)建運(yùn)行時(shí)報(bào)錯(cuò)問(wèn)題及解決

    這篇文章主要介紹了react腳手架構(gòu)建運(yùn)行時(shí)報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • React使用hook如何實(shí)現(xiàn)數(shù)據(jù)雙向綁定

    React使用hook如何實(shí)現(xiàn)數(shù)據(jù)雙向綁定

    這篇文章主要介紹了React使用hook如何實(shí)現(xiàn)數(shù)據(jù)雙向綁定方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評(píng)論

琼结县| 天祝| 镇安县| 高阳县| 庆城县| 越西县| 淅川县| 凯里市| 右玉县| 锡林浩特市| 汕头市| 都匀市| 邳州市| 青岛市| 古交市| 禹城市| 罗平县| 南昌县| 翁牛特旗| 仙桃市| 佳木斯市| 桓台县| 德昌县| 精河县| 嘉定区| 昂仁县| 泰和县| 新和县| 门源| 塔河县| 台中县| 兴山县| 广河县| 永康市| 伊吾县| 邵东县| 东平县| 团风县| 平武县| 乐平市| 台东县|