React Icons React에서 아이콘을 사용하는 가장 쉬운 방법

7378 단어 reacticonsnpmtutorial

목차



    * [Overview](#chapter-1)
    * [Getting Started](#chapter-2)
    * [Step 1](#chapter-3)
    * [Step 2](#chapter-4)
    * [Step 3](#chapter-4)
    * [Conclusion](#chapter-5)

개요

Today we'll be discussing the easiest way to use SVG icons in react. We'll be using an npm package that will enable us to use icons from some of the most well-known icon libraries out there.

The package we'll be discussing is called React-Icons.
Link to npm page:
https://www.npmjs.com/package/react-icons

시작하기

If you'll be following along feel free to clone this repository, as this is what we'll be using for our boilerplate: https://github.com/kevinsims1/bare-react

이 시점부터 저장소를 복제했다고 가정합니다.

1 단계

Open the terminal in the root of the project and use this command:
npm install react-icons

This will download our package and give us full accessibility to it.

2 단계

Now open the boilerplate in your code editor.

React Icons give you the option to choose icons from all of the following icon libraries:



이를 사용하려면 라이브러리와 관련된 가져오기를 사용하기만 하면 됩니다.

다음은 각 관련 가져오기의 예입니다.



bare.js 파일로 이동하여 1행 뒤에 다음 가져오기를 추가합니다.

import {FaTwitter} from "react-icons/fa"


이제 return 문을 삭제하고 다음을 추가합니다.

const Bare = () => {
  return (
    <div>
      <FaTwitter />
    </div>
  )
}


터미널로 이동하여 다음을 입력합니다. npm run start
페이지 왼쪽 상단에 이 아이콘이 있는 페이지가 표시됩니다.


작동 중입니다!

하지만... 약간 밋밋한 편입니다.

3단계

React Icons handles that by allowing you to import an IconContext component that allows you to do a lot of cool things to your icon. We'll be using it to add some style to ours.

Add this import after our first one:

import {IconContext} from "react-icons"

Now wrap our icon in the IconContext component like so:

const Bare = () => {
  return (
   <IconContext.Provider>
    <div>
      <FaTwitter />
    </div>
   </IconContext.Provider>
  )
}

Now all we need to do is add a value prop to our IconContext component, pass it an object. Inside of that object add a key called style, assign it the value of an object and start styling.

Example:

const Bare = () => {
  return (
   <IconContext.Provider value={{ style: {fontSize: '30px', color: "rgb(0, 123, 255)"}}}>
       <div>
         <FaTwitter />
       </div>
    </IconContext.Provider>
  )
}

If you head back to your browser you should now see this:



결론

That is all for this tutorial. If you've enjoyed it please take a moment to leave a like, to help the next dev. Thank you.

좋은 웹페이지 즐겨찾기