简体   繁体   English

如何在 React JS 中使用天气图标?

[英]How to use weather-icons with React JS?

So I am trying to use weather-icons in a React application.所以我想在 React 应用程序中使用天气图标

I downloaded the package and added it to my solution ( here ).我下载了package并将其添加到我的解决方案中( 此处)。

(I use react-redux-universal-hot-example starter kit) (我使用react-redux-universal-hot-example入门工具包)

In a Forecast.js container I added the following:Forecast.js容器中,我添加了以下内容:

 import React, {Component, PropTypes} from 'react'; import {connect} from 'react-redux'; import {fetchForecast} from 'redux/modules/weatherForecast'; @connect( state => ({forecast: state.weatherForecast.forecast}), {fetchForecast} ) export default class Forecast extends Component { render() { return ( <div> <i className="wi wi-day-sunny"></i> </div>); } }

It doesn't work yet...它还没有用...

  1. How do I reference weather-icons so these classes are recognized in my container?我如何引用天气图标以便在我的容器中识别这些类?
  2. Do I need css and less folders?我需要 css 和更少的文件夹吗? (I would prefer to use scss since starter kit is using this) (我更愿意使用 scss,因为入门工具包正在使用它)

My code structure:我的代码结构:

[src]
 |--[containers]
 |       |--[Forecast]
 |              |--Forecast.js
 |
 |--[weather-icons]
        |--[css]
        |--[font]
        |--[less]
        |--[sass]
        |--[values]

My source code我的源代码

One way is importing the css file directly; 一种方法是直接导入css文件。 (As stated here ) (如前所述这里

import 'weather-icons/css/weather-icons.css';

Note: Path can be changed. 注意:路径可以更改。 I've installed the weather-icons package through npm which means it is under node_modules. 我已经通过npm安装了weather-icons软件包,这意味着它位于node_modules下。

And After importing the css file you can use it like below; 导入css文件后,您可以像下面这样使用它;

<i className="wi wi-day-lightning"></i>

[2023] [2023]

If someone is doing this is Next.js which does not allow to import global css, just use css modules!如果有人这样做是 Next.js 不允许导入全局 css,只需使用 css 模块!

import is from "../../styles/weather-icons.min.module.css"

And use it like this even with tailwind:D即使顺风也可以像这样使用它:D

<i className={`${is.wi} ${is["wi-day-cloudy"]} text-xl text-cyan-300`}></i>

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM