zoukankan      html  css  js  c++  java
  • React使用useLocation监听url地址变化,工具URLSearchParams获取参数

    import * as React from 'react'
    import { Link, useLocation } from 'react-router-dom'
    export interface BottomNavigationViewProps {
      className?: string
      isLogin?: boolean
      avatar?: string
      userMenuOpen?: boolean
      hasNotification?: boolean
      onUserMenuOpen?: () => void
      onUserMenuClose?: UserMenuProps['onUserMenuClose']
      onLoginClick?: (event: React.MouseEvent<HTMLButtonElement>) => void
    }
    const NAVIGATION_ITEMS = [
      {
        id: 'home',
        icon: 'home',
        href: '/',
        path: /^/$/,
        name: '首页',
      },
      {
        id: 'discuss',
        icon: 'discuss',
        href: '/discussion',
        path: /^/discussion/,
        name: '讨论',
      },
      // {
      //   id: 'purchased',
      //   icon: 'myShow',
      //   href: '/purchased',
      //   path: /^/purchased/,
      //   name: '我的项目',
      // },
    ]
    
    /**
     * 一个底栏导航组件,仅在移动端展示。某种 Material Design 原教旨主义玩家公敌。
     * @param props 向组件内部传入的参数。
     * @param props.className (可选)为本组件额外追加的 class 名称。
     */
    const BottomNavigationView: React.FC<BottomNavigationViewProps> = ({
      className,
      isLogin,
      avatar,
      userMenuOpen,
      onUserMenuOpen,
      onUserMenuClose,
      onLoginClick,
      hasNotification,
      ...props
    }) => {
      const mainClassName = cn(styles.bottomNavigation, className)
      const location = useLocation()
      const utm = new URLSearchParams(location.search)
      if (utm.get("utm_source")) {
        localStorage.setItem("utm_source",utm.get("utm_source")||"")
      }
      return (
        <>
          <i className={styles.placeHolder} />
          <div className={mainClassName}>
            <nav className={styles.nav}>
              {NAVIGATION_ITEMS.map((navigationItem) => (
                <li
                  key={navigationItem.id}
                  className={cn({
                    [styles.current]: location.pathname.match(navigationItem.path),
                  })}
                >
                  <Link className={styles.link} to={navigationItem.href}>
                    <span className={styles.iconContainer}>
                      <Icon name={navigationItem.icon} />
                    </span>
                    <span className={styles.label}>{navigationItem.name}</span>
                  </Link>
                </li>
              ))}
    
              <li
                className={cn({
                  [styles.current]: location.pathname.match(//(settings|member)$/),
                })}
              >
                {isLogin ? (
                  <div className={cn(styles.link, styles.withUserMenu)}>
                    <Button
                      className={cn(
                        styles.userMenuLinkContainer,
                        styles.buttonLink
                      )}
                      color="content"
                      onClick={() => onUserMenuOpen && onUserMenuOpen()}
                    >
                      <RedDot show={hasNotification}>
                        <span className={styles.iconContainer}>
                          <Avatar alt="您的头像" src={avatar} size="w20" />
                        </span>
                      </RedDot>
                      <span className={styles.label}>我的</span>
                    </Button>
                    <div className={styles.userMenuContainer}>
                      <UserMenu
                        showUserRightButton
                        className={styles.userMenu}
                        open={userMenuOpen}
                        corner="bottom"
                        hasNotification={hasNotification}
                        onUserMenuClose={() => onUserMenuClose && onUserMenuClose()}
                      />
                    </div>
                  </div>
                ) : (
                  <Button
                    className={cn(styles.link, styles.buttonLink)}
                    color="content"
                    onClick={onLoginClick}
                  >
                    <span className={styles.iconContainer}>
                      <Icon name="userCenter" />
                    </span>
                    <span className={styles.label}>我的</span>
                  </Button>
                )}
              </li>
            </nav>
          </div>
        </>
      )
    }
    
    export default React.memo(BottomNavigationView)
  • 相关阅读:
    C#读写xml文件
    实现SQL_SERVER的双机实时备份
    Microsoft .NET Pet Shop 4 架构与技术分析
    xp系统运行asp.net时候出现“服务器应用程序不可用”的必杀
    获取服务器根域名
    C#如何创建Xml文件
    用C#创建XML[简单代码]
    关闭Viewstate
    数字签名
    IE插件
  • 原文地址:https://www.cnblogs.com/kangxinzhi/p/14987588.html
Copyright © 2011-2022 走看看