简体   繁体   English

反应图标和反应路由器

[英]react-icons and react Router

Context语境

i'm designing a navigation bar for my application and my proyect uses react-router and react-router-dom for managing routing and react-icons for icon retrieving.我正在为我的应用程序设计一个导航栏,我的项目使用react-routerreact-router-dom来管理路由和用于图标检索的 react-icons

Code代码

import React from 'react';
import { NavLink } from 'react-router-dom';
import { MdAssessment } from 'react-icons/md'
import Styles from './NavBar.module.css' 

const NavBar = (props) => {

    return (
        <nav className={Styles.navBar}>
            {navIcon}
            <NavLink
                activeStyle={{ color : 'red' }}
                className={`${Styles.navBar__Dashboard} `}
                exact to="/">
                <MdAssessment />
            </NavLink>
            <NavLink
                activeStyle={{ color: 'red'  }}
                className={Styles.navBar__Requests}
                to="/requests">
                Requests
            </NavLink>
            <NavLink
                activeStyle={{ color: 'red'  }}
                className={Styles.navBar__Tasks}
                to="/tasks">
                Tasks
            </NavLink>
        </nav>
    )
}

export default NavBar

Problem问题

I'm trying to extrapolate activeStyle functionality of NavLink to my icon.我正在尝试将 NavLink 的activeStyle功能外推到我的图标。 Given that react-icons provides you with component as icons, i'm struggling with which approach should i use:鉴于 react-icons 为您提供了作为图标的组件,我正在努力使用哪种方法:

  1. Should i wrapped in a HOC?我应该包裹在 HOC 中吗?
  2. Should i encapsulate in a custom Hook?我应该封装在自定义 Hook 中吗?

Notes笔记

I've tried with both approaches but can't get my head arround it.我已经尝试了这两种方法,但无法理解它。 I've tried implementing useRef , useEffect while reading to location with useLocation hook as well.我尝试在使用useLocation钩子读取位置时实现useRefuseEffect Any suggestion for a better implementation of a generic NavIcon feature taking this context into account?考虑到这种情况,有什么建议可以更好地实现通用NavIcon功能?

By using curly brackets you are importing the icon as a named import.通过使用大括号,您将图标作为命名导入导入。 So the import only works if the file contains a named export of the same name you have assigned it.因此,仅当文件包含与您分配的名称相同的命名导出时,导入才有效。 See this question for more info.有关更多信息,请参阅此问题

According to the package documentation , this is the correct way to import an icon.根据package 文档,这是导入图标的正确方法。 Can you check to make sure the named export is the same as you have imported?您可以检查以确保命名的导出与您导入的相同吗?

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

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