1

Hello I am a beginner getting started with React Router -v^6.2.2, and generated the basic sources with create-react-app on template typescript -v4.6.2.

It works but I have this error on my console Visual studio code.

I have a problem on exact and activeClassName on the tag NavLink.

Error message:

"Type '{ children: string; className: string; activeClassName: string; exact: true; to: string; }' is not assignable to type 'IntrinsicAttributes & NavLinkProps & RefAttributes'. Property 'activeClassName' does not exist on type 'IntrinsicAttributes & NavLinkProps & RefAttributes'. "

import { NavLink } from "react-router-dom"; import '../styles/styles-pages/navigation.scss'

export const Navigation = () => { return ( Home Discution ) }

1 Answer 1

1

In react-router-dom@6 the NavLink component hasn't any activeClassName and exact props.

NavLink

declare function NavLink(
  props: NavLinkProps
): React.ReactElement;

interface NavLinkProps
  extends Omit<
    LinkProps,
    "className" | "style" | "children"
  > {
  caseSensitive?: boolean;
  children?:
    | React.ReactNode
    | ((props: { isActive: boolean }) => React.ReactNode);
  className?:
    | string
    | ((props: {
        isActive: boolean;
      }) => string | undefined);
  end?: boolean;
  style?:
    | React.CSSProperties
    | ((props: {
        isActive: boolean;
      }) => React.CSSProperties);
}

Remove the activeClassName prop from your links. If you still need to apply an active class then use a function on the className prop and access the isActive prop.

import { NavLink } from "react-router-dom";
import '../styles/styles-pages/navigation.scss'

export const Navigation = () => {
  return (
    <div id="navigation_div_container">
        <NavLink
          className={({ isActive }) =>
            [
              "link_nav",
              isActive ? "active" : null,
            ]
              .filter(Boolean)
              .join(" ")
          }
          end // <-- prevents matching on sub-routes, similar to exact
          to="/"
        >
          Home
        </NavLink>
        <NavLink
          className={({ isActive }) =>
            [
              "link_nav",
              isActive ? "active" : null,
            ]
              .filter(Boolean)
              .join(" ")
          }
          to="/discution"
        >
          Discution
        </NavLink>
    </div>
  );
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your prompt response. I understand better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.