简体   繁体   English

在 React Router 中更改 NavLink 中嵌套 div 的背景颜色

[英]Change the backgroundColor of nested div in NavLink in React Router

I'm using react-router-dom@6.5.0 but unable to style the NavLink component backgroundColor when route is active.我正在使用react-router-dom@6.5.0但无法在路由处于活动状态时设置NavLink组件 backgroundColor 的样式。 This works when I insert some text just below NavLink tag.当我在NavLink标签下方插入一些文本时,这会起作用。

    import {
  Box,
  CloseButton,
  Flex,
  Icon,
  useColorModeValue,
  Drawer,
  DrawerContent,
  Text,
  useDisclosure,
  BoxProps,
  FlexProps,
} from '@chakra-ui/react';

import {
  FiHome,
  FiCalendar,
  FiBriefcase,
  FiDollarSign,
} from 'react-icons/fi';


import {
  IoPawOutline
} from 'react-icons/io5';

import { IconType } from 'react-icons';

import { NavLink, Outlet } from "react-router-dom";

import Navbar from './Navbar';

<NavLink 
  to={to}
  style={({ isActive })=> ({
    background: isActive ? "green" : "blue",
  })}  
>  
  <Flex
    align="center"
    p="4"
    mx="4"
    borderRadius={["4px"]}
    role="group"
    cursor="pointer"
    _hover={{
      bg: '#1D3EAC',
      color: 'white',
    }}
    _active={{
      bg: '#1D3EAC',
      color: 'white',
    }}
  >
    {icon && (
      <Icon
        mr="4"
        fontSize="16"
        _groupHover={{
          color: 'white',
        }}
        as={icon}
      />
    )}
    {children}
  </Flex>
</NavLink>

You can try to use function as a children for NavLink.您可以尝试使用 function 作为 NavLink 的孩子。 One function params is boolean isActive .一个 function 参数是 boolean isActive

<NavLink to="/home">
  {({ isActive }) => (
    <span style={{ backgroundColor: isActive ? 'red' : 'blue'}}>link</span>
  )}
</NavLink>

You can use the children render function of the NavLink to conditionally set the background color of the Flex component.您可以使用 NavLink 的children渲染NavLink来有条件地设置Flex组件的背景颜色。

Example:例子:

<NavLink to={to}>
  {({ isActive }) => (
    <Flex
      align="center"
      p="4"
      mx="4"
      borderRadius={["4px"]}
      role="group"
      cursor="pointer"
      _hover={{
        bg: "#1D3EAC",
        color: "white"
      }}
      _active={{
        bg: "#1D3EAC",
        color: "white"
      }}
      color="white"
      bg={isActive ? "green" : "blue"}
    >
      {icon && (
        <Icon
          mr="4"
          fontSize="16"
          _groupHover={{
            color: "white"
          }}
          as={icon}
        />
      )}
      {children}
    </Flex>
  )}
</NavLink>

在此处输入图像描述

You could also render the Flex component as a NavLink .您还可以将Flex组件呈现as NavLink

Example:例子:

<Flex
  as={NavLink}
  to={to}
  align="center"
  p="4"
  mx="4"
  borderRadius={["4px"]}
  role="group"
  cursor="pointer"
  _hover={{
    bg: "#1D3EAC",
    color: "white"
  }}
  _active={{
    bg: "#1D3EAC",
    color: "white"
  }}
  color="white"
  style={({ isActive }) => ({
    background: isActive ? "green" : "blue"
  })}
>
  {icon && (
    <Icon
      mr="4"
      fontSize="16"
      _groupHover={{
        color: "white"
      }}
      as={icon}
    />
  )}
  {children}
</Flex>

在此处输入图像描述

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

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