【问题标题】:How can I change the color of the active navbar item?如何更改活动导航栏项目的颜色?
【发布时间】:2021-01-30 20:13:50
【问题描述】:

我的导航栏上有 3 个按钮,如果它是活动页面,我想将文本的颜色更改为不同的颜色。

我还想要一个默认的,第一个。

StyledLink 来自另一个答案,但它不起作用。我正在使用脉轮 ui。

这是我的 layout.js 代码:

const StyledLink = styled(Nav.Link)`
  font-size: 12px;
  text-transform: uppercase;
  color: ${(props) => (props.active ? "red" : "inherit")};
`;

export default function Layout({
  children,
  title = "This is the default title",
  props
}) {
  return (
    <Flex border="1px solid #EDF2F7" padding="2.5px 24px">
      <Text fontWeight="bold" color="orangered" fontSize="35px" mt="-5px">
        Index
      </Text>
      <Nav>
        <StyledLink href="/" style={{ textDecoration: "none" }}>
          <Text ml="70px" mt="1px"  fontSize="20px" isTruncated > //color="orangered"
            LIVE Map
          </Text>
        </StyledLink>
      </Nav>
      <Link href="/requests" style={{ textDecoration: "none" }}>
        <Text ml="70px" mt="1px" fontSize="20px" isTruncated>
          Request List
        </Text>
      </Link>
      <Link style={{ textDecoration: "none" }}>
        <Text ml="50px" mt="1px" fontSize="20px">
          Analytics
        </Text>
      </Link>
    </Flex>
  );
}

我是nextjs的新手,反应过来,我似乎找不到正确的答案

【问题讨论】:

    标签: javascript reactjs next.js chakra


    【解决方案1】:

    您需要使用useRouter。在这里阅读更多:https://nextjs.org/docs/api-reference/next/router

    您可以使用它来确定您当前所在的路径名并动态更改样式。下面的代码取自我的网站:https://github.com/bjcarlson42/benjamincarlson.io/blob/master/components/Navigation.js

    import { useRouter } from 'next/router'
    
    // your code here
    
     const { colorMode } = useColorMode()
     const router = useRouter()
    
        const bgColor = {
            light: '#fff',
            dark: '#171717' // #1A202C
        }
    
    <NextLink href="/statistics" passHref>
                        <Button
                            as="a"
                            variant="ghost"
                            p={[1, 2, 4]}
                            _hover={{ backgroundColor: navHoverBg[colorMode] }}
                            backgroundColor={router.pathname === '/statistics' ? navHoverBg[colorMode] : null}
                            aria-label="Statistics"
                        >
                            Statistics
                        </Button>
                    </NextLink>
                    <NextLink href="/blog" passHref>
                        <Button
                            as="a"
                            variant="ghost"
                            p={[1, 2, 4]}
                            _hover={{ backgroundColor: navHoverBg[colorMode] }} backgroundColor={router.pathname.includes('/blog') ? navHoverBg[colorMode] : null}
                            aria-label="Blog"
                        >
                            Blog
                        </Button>
                    </NextLink>
                    <NextLink href="/projects" passHref>
                        <Button
                            as="a"
                            variant="ghost"
                            p={[1, 2, 4]}
                            _hover={{ backgroundColor: navHoverBg[colorMode] }} backgroundColor={router.pathname === '/projects' ? navHoverBg[colorMode] : null}
                            aria-label="Projects"
                        >
                            Projects
                        </Button>
                    </NextLink>
    
    // more code here
    

    现在,问题的第二部分:如何将第一个设为默认值?

    您可以通过添加更多逻辑来做到这一点。回到上面的例子,我会默认统计。

    <NextLink href="/statistics" passHref>
                        <Button
                            as="a"
                            variant="ghost"
                            p={[1, 2, 4]}
                            _hover={{ backgroundColor: navHoverBg[colorMode] }}
                            backgroundColor={router.pathname === '/statistics' || (router.pathname != '/statistics' && router.pathname != '/blog' && router.pathname != '/projects') ? navHoverBg[colorMode] : null}
                            aria-label="Statistics"
                        >
                            Statistics
                        </Button>
                    </NextLink>
    

    如您所见,我正在检查我们是否在统计页面上,或者我们是否不在这 3 个页面中的任何一个上 - 统计信息仍将处于“活动”状态,因为这是默认设置。

    【讨论】:

      【解决方案2】:

      您的问题没有足够的信息,但我已经完成了。

      你可以这样做

      const history = useHistory()
      <StyledComponent isActive={history.location.pathname === '/'} href='/'>
      </StyledComponent>
      

      【讨论】:

      • 你能在答案中更详细吗?我可以在这个组件中使用 useHistory() 吗?它给了我一个错误。
      • useHistory 是 react-router-dom 钩子,您可以在反应函数中使用。
      • StyledComponent 不能这样工作
      【解决方案3】:

      您的 StyledLink 期望 active 道具改变其颜色。您可以使用useRouter 来检查当前路径,然后在此基础上将active 传递给StyledLink

      import { useRouter } from "next/router";
      import Link from "next/link";
      
      // ...
      
      export default function Layout({ children, title = "This is the default title", props }) {
          const { asPath } = useRouter();
      
          return (
              // ...
              <Link href="/" passHref>
                  <StyledLink style={{ textDecoration: "none" }} active={asPath === "/"}>
                      <Text ml="70px" mt="1px"  fontSize="20px" isTruncated > //color="orangered"
                          LIVE Map
                      </Text>
                  </StyledLink>
              </Link>
              // ...
          );
      }
      

      【讨论】:

      • 风格化(文本)。带有“styled(Nav.Link)”或“styled(Link)”的 const StyledLink 不起作用。它仅适用于 styled(Text) 但然后 href 的功能不起作用,它充当普通文本。会是什么?
      • LIVE Map 我明白了以这种方式工作,但如果我点击链接,它不会更新。
      • 会将passHref 添加到您的Link 工作中吗?例如:&lt;Link href="/" passHref&gt;...&lt;/Link&gt;。如果是这样,我会更新我的答案以反映这一点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      相关资源
      最近更新 更多