React17 使用 JSX 的情況下無須再顯式導入 React


React 17 引入了新的 JSX 編譯方式,無須在組件中顯式地 import React。注意需要配合 TypeScript 4.1+ 版本。

亦即,使用 React 17+ 的項目中 TypeScript 會有如下對應的提示:

'React' is declared but its value is never read.ts(6133)
 

Screen Shot 2021-05-12 at 7 46 34 PM

但移除 React 的導入后,又會報如下的 ESLint 錯誤:


Screen Shot 2021-05-12 at 9 38 27 PM

根據 React 文檔 的描述,可關閉對應的 ESLint 規則,因為是不必要的了。

{
  // ...
  "rules": {
    // ...
    "react/jsx-uses-react": "off",
    "react/react-in-jsx-scope": "off"
  }
}
 

相關資源

The text was updated successfully, but these errors were encountered:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM