React 学习笔记_23(在React中使用Icons)

简介

Font Awesome 是一个基于CSS和LESS的字体和图标工具套件,常常能够在HTML中加入一些图标,但是在React中是使用JSX来撰写HTML的物件,它与一般的HTML不一样所以无法直接使用Font Awesome,本篇介绍如何利用别种方法在React中使用Font Awesome。

react-fontawesome

安装

Using NPM:

npm i --save @fortawesome/fontawesome-svg-corenpm i --save @fortawesome/free-solid-svg-iconsnpm i --save @fortawesome/react-fontawesome

Yarn:

yarn add @fortawesome/fontawesome-svg-coreyarn add @fortawesome/free-solid-svg-iconsyarn add @fortawesome/react-fontawesome

使用

在import中需要引入需要的图标名称,输入的规则 : 若名称为fas fa-ato 则import {faAtom}
http://img2.58codes.com/2024/20124767g9jnSc1SQx.png

import React from "react";import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'import { faAtom } from '@fortawesome/free-solid-svg-icons'class App extends React.Component{    render()    {        return(            <div className="icon">                <FontAwesomeIcon icon={faAtom} />            </div>        )    }}export default App;

结果 : http://img2.58codes.com/2024/20124767IyVp6iMuLG.png

改变样式

使用Font Awesome所提供的Icon组件有支援SASS来更改其样式。

.icons{    svg{        color: #000;        width: 50px;        height: 50px;    }}

结果 : http://img2.58codes.com/2024/20124767VAADophJNc.png

react-icons

安装

Using NPM :

npm install react-icons --save

Yarn :

yarn add react-icons

使用

在 react-icons 官网中可以使用多种类型的图案,而不同种类的图案也需要import不同的Component。

http://img2.58codes.com/2024/201247670ZLYTo779h.png

import React from "react";import { BsFillArchiveFill  } from "react-icons/bs";class App extends React.Component{    render()    {        return(            <div className = "icon">                <BsFillArchiveFill />            </div>        )    }};export default App;

结果 : http://img2.58codes.com/2024/201247673HVxrohfQy.png

改变样式

使用react-icons功能依样能够使用SASS来改变其样式

.icons{    svg{        color: #00f;        width: 50px;        height: 50px;    }}

结果 : http://img2.58codes.com/2024/20124767cFSL8I7SyD.png
http://img2.58codes.com/2024/2012476774AuWI9HIJ.png

参考资料 :
react-fontawesome/GitHub
react-icons/GitHub
react-icons


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章