Carbon: HeaderMenuItem element bug with typescript

Created on 20 Mar 2020  路  4Comments  路  Source: carbon-design-system/carbon

Hello, I am using the latest version of carbon-components-react v7.10.2 with typescript.

I have the following issue, when I pass element prop element={NavLink} and I got the following error:

Screenshot 2020-03-20 at 11 15 39

ui-shell dev 馃 bug 馃悰

Most helpful comment

To anyone who finds this.

@ilievZlatko's problem occurs when using Carbon in a TS project.

Your issue is probably related to this:
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/44772

You just need to import the LinkProps from react-router-dom (it will probably work with NavLinkProps as well) and use it with the Carbon component.
It even works with React Router v6.0.0-alpha.5 (yarn add react-router@next react-router-dom@next as of today)

Example:

import { Link, LinkProps } from 'react-router-dom';

const MainHeader: React.FC = () => (
  <Header aria-label="Carbon Tutorial">
    <SkipToContent />
    <HeaderName<LinkProps> element={Link} to="/" prefix="IBM">
      Carbon Tutorial
    </HeaderName>
    <HeaderNavigation aria-label="Carbon Tutorial">
      <HeaderMenuItem<LinkProps> element={Link} to="repos">
        Repositories
      </HeaderMenuItem>
    </HeaderNavigation>
    <HeaderGlobalBar>
      <HeaderGlobalAction aria-label="Notifications">
        <Notification20 />
      </HeaderGlobalAction>
      <HeaderGlobalAction aria-label="User Avatar">
        <UserAvatar20 />
      </HeaderGlobalAction>
      <HeaderGlobalAction aria-label="App Switcher">
        <AppSwitcher20 />
      </HeaderGlobalAction>
    </HeaderGlobalBar>
  </Header>
);

export default MainHeader;

Image of the code above:

Screen Shot 2020-06-09 at 21 58 56

All 4 comments

can you create a reduced test case in Code Sandbox (with TS) for easier testing and debugging?

Closing due to inactivity. Also - Seems that <HeaderMenuItem> doesn't take element prop.

To anyone who finds this.

@ilievZlatko's problem occurs when using Carbon in a TS project.

Your issue is probably related to this:
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/44772

You just need to import the LinkProps from react-router-dom (it will probably work with NavLinkProps as well) and use it with the Carbon component.
It even works with React Router v6.0.0-alpha.5 (yarn add react-router@next react-router-dom@next as of today)

Example:

import { Link, LinkProps } from 'react-router-dom';

const MainHeader: React.FC = () => (
  <Header aria-label="Carbon Tutorial">
    <SkipToContent />
    <HeaderName<LinkProps> element={Link} to="/" prefix="IBM">
      Carbon Tutorial
    </HeaderName>
    <HeaderNavigation aria-label="Carbon Tutorial">
      <HeaderMenuItem<LinkProps> element={Link} to="repos">
        Repositories
      </HeaderMenuItem>
    </HeaderNavigation>
    <HeaderGlobalBar>
      <HeaderGlobalAction aria-label="Notifications">
        <Notification20 />
      </HeaderGlobalAction>
      <HeaderGlobalAction aria-label="User Avatar">
        <UserAvatar20 />
      </HeaderGlobalAction>
      <HeaderGlobalAction aria-label="App Switcher">
        <AppSwitcher20 />
      </HeaderGlobalAction>
    </HeaderGlobalBar>
  </Header>
);

export default MainHeader;

Image of the code above:

Screen Shot 2020-06-09 at 21 58 56

To anyone who finds this.

Thanks for the help.
It's too bad new users of carbon like me are stuck on this problem when following the official tutorial because of Typescript.
https://www.carbondesignsystem.com/developing/react-tutorial/step-1/#add-routing

Was this page helpful?
0 / 5 - 0 ratings