2022 第31週 學習筆記:在 react 專案透過 @svgr/webpack 引用 .svg 檔案

總結

記錄一下如何透過 @svgr/webpack 在 React 專案中操作 .svg 檔案,使用 @svgr/webpack 的好處是「同一份 .svg 檔案,可以選擇以『元件』或『路徑』的格式來使用」

程式碼

筆記

關於 svg tag 中的 title

MDN: Text in a <title> element is not rendered as part of the graphic, but browsers usually display it as a tooltip. If an element can be described by visible text, it is recommended to reference that text with an aria-labelledby attribute rather than using the <title> element.

如果有提供的話,瀏覽器會將其應用於 tooltip 的展示文字;但(參考 MDN 表格) FireFox 以外的支援度並不明朗

關於 react-app-env.d.ts

This file references TypeScript types declarations that are specific to projects started with Create React App. These type declarations add support for importing resource files such as bmp, gif, jpeg, jpg, png, webp, and svg.

其實就是各種格式的型別定義集合

關於 Triple-Slash Directives

/// <reference types="..." />

Triple-slash directives are single-line comments containing a single XML tag. The contents of the comment are used as compiler directives. Triple-slash references instruct the compiler to include additional files in the compilation process.

a /// <reference types="..." /> directive declares a dependency on a package. For example, including /// <reference types="node" /> in a declaration file declares that this file uses names declared in @types/node/index.d.ts; and thus, this package needs to be included in the compilation along with the declaration file.

提示編譯器在執行編譯時,需要額外引用哪些套件的型別定義檔案

參考文件