여러 아이콘을 사용할 수 있는 대표적인 서비스인 Font Awesome을 리액트에서 사용하는 법입니다.
Add Icons with React
The internet's icon library + toolkit. Used by millions of designers, devs, & content creators. Open-source. Always free. Always awesome.
fontawesome.com
자세한 내용은 위의 링크에서 확인할 수 있습니다. 저는 위 방법을 그대로 따라해 보겠습니다.
npm i --save @fortawesome/fontawesome-svg-core
npm i --save @fortawesome/free-solid-svg-icons
npm i --save @fortawesome/free-regular-svg-icons
저는 무료버전의 아이콘을 사용할 것이기 때문에 free icon을 인스톨 해 주었습니다.
npm i --save @fortawesome/react-fontawesome@latest
npm i babel-plugin-macros
위 두개를 실행해 준 다음. 진행중인 프로젝트의 루트 디렉토리로 이동합시다.
루트 디렉토리에 babel.config.js파일과 babel-plugin-macros.config.js파일을 만들어 아래와 같이 작성해 줍시다.
//babel.config.js
module.exports = function (api) {
return {
plugins: ['macros'],
}
}
//babel-plugin-macros.config.js
module.exports = {
'fontawesome-svg-core': {
'license': 'free'
}
}
이제 모든 준비가 완료되었습니다.
Font Awesome 홈페이지에서 마음에 드는 아이콘을 골라서 확인해줍시다.
위와 같이 <FontAwesomeIcon icon="fa-solid fa-plus"> 라고 나오는데, 저렇게 적지 마시고,
아래와 같이 적어줍시다.
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { icon } from "@fortawesome/fontawesome-svg-core/import.macro";
...
<FontAwesomeIcon
icon={icon({ name: "plus", style: "solid" })}
style={{ height: 21, width: 21 }}
/>
이렇게 아이콘을 사용할 수 있습니다.
참고로 아이콘의 name prop은 사용하고자 하는 아이콘의 이름이고,
style과 family props의 type은 아래와 같습니다.
export type IconMacroParams = {
name: IconName,
style?: IconStyle,
family?: IconFamily
};
export type IconFamily = "classic" | "sharp" | "duotone";
export type IconStyle = "solid" | "regular" | "light" | "thin" | "duotone" | "brands" ;
'Front End > React' 카테고리의 다른 글
React 에서 Swiper.js를 이용하여 캐러셀 Carousel 만들기 TS JS (0) | 2023.06.23 |
---|---|
React 에서 setInterval 사용하는 법 리액트 JS TS (0) | 2023.06.23 |
React 에서 setInterval, timer 사용하는 법 리액트 JS TS (0) | 2023.06.22 |
CRA(create-react-app)환경에서 .env, 환경 변수 사용하는 방법 (0) | 2023.06.16 |
React에서 React-Hook-Form을 이용할 때 focus 하는 법. 리액트 JS, TypeScript (0) | 2023.06.02 |