0

I am using React and creating a menu. I have all the submenus working and pointing to internal links however when I try and add an external link to the menu it always ends up having a / in front.

If you look below at '''<MenuItem title={intl.formatMessage({id: 'MENU.NBSONLINE'})} to='http://www.northernbalanceonline.com' /> ''''

it rendors the URL of /www.northernbalance.com
How do I remove the "/" in the url? See code below.

'''

import React, { useState, useEffect } from 'react'
import { shallowEqual, useSelector } from 'react-redux'
import {MenuItem} from './MenuItem'
import { MenuInnerWithSub } from './MenuInnerWithSub'
import {useIntl} from 'react-intl'
import { UserModel } from '../../../../types'
import { RootState } from '../../../../setup'
import * as levelHelper from '../../../../helper/level.helper'


export function MenuInner() {
  const [isAdmin, setIsAdmin] = useState<boolean>(false)

  const user: UserModel = useSelector<RootState>(({auth}) => auth.user, shallowEqual) as UserModel

  useEffect(() => {
    setIsAdmin(levelHelper.isNBS(user?.type))
  }, [user])

  const intl = useIntl()
  return (
    <>
      <MenuItem title={intl.formatMessage({id: 'MENU.DASHBOARD'})} to='/dashboard' />
      <MenuItem title={intl.formatMessage({id: 'MENU.NBSONLINE'})} to='http://www.northernbalanceonline.com' />
      {
        isAdmin ?
        <MenuInnerWithSub
          title='Learning Center'
          to='/learning-center'
          menuPlacement='bottom-start'
          menuTrigger='click'
        >
          <MenuItem 
            to='/learning-center/employees' 
            title='Employees' 
            hasBullet={true} 
          />
          <MenuItem 
            to='/learning-center/customer' 
            title='Customers' 
            hasBullet={true} 
          />
        </MenuInnerWithSub> :
        <MenuItem to='/learning-center/customer' title='Learning Center' />
      }
    </>
  )
}

'''

'''' Here is the MenuItem.tsx

import React from 'react'
import {Link} from 'react-router-dom'
import {useLocation} from 'react-router'
import clsx from 'clsx'
import {checkIsActive, KTSVG} from '../../../helpers'

type Props = {
  to: string
  title: string
  icon?: string
  fontIcon?: string
  hasArrow?: boolean
  hasBullet?: boolean
}

const MenuItem: React.FC<Props> = ({
  to,
  title,
  icon,
  fontIcon,
  hasArrow = false,
  hasBullet = false,
}) => {
  const {pathname} = useLocation()

  return (
    <div className='menu-item me-lg-1'>
      
      <Link
        className={clsx('menu-link py-3', {
          active: checkIsActive(pathname, to),
        })}
       
        to={to}
      >
        {hasBullet && (
          <span className='menu-bullet'>
            <span className='bullet bullet-dot'></span>
          </span>
        )}

        {icon && (
          <span className='menu-icon'>
            <KTSVG path={icon} className='svg-icon-2' />
          </span>
        )}

        {fontIcon && (
          <span className='menu-icon'>
            <i className={clsx('bi fs-3', fontIcon)}></i>
          </span>
        )}

        <span className='menu-title'>{title}</span>

        {hasArrow && <span className='menu-arrow'></span>}
      </Link>
    </div>
  )
}

export {MenuItem}

''''

1 Answers1

1

Maybe you don't need this anymore but the only way I found is to do something like this:

<MenuItem title={intl.formatMessage({id: 'MENU.NBSONLINE'})} to='' onClick={() => (window.location.href = 'www.northernbalanceonline.com')} />