8

I am using typescript in a React project and I want to add tag's props. I can access the properties of the link element but not the properties a element. I am getting an error when the properties of the link tag use an a tag.

Is there an 'HTMLAElement' structure for an element a in React?

const MyComponent: FunctionComponent<React.LinkHTMLAttributes<HTMLLinkElement>> = (
  props: React.LinkHTMLAttributes<HTMLLinkElement>,
) => {
  return (
      <a {...props} className="mycomponent">  // When I use it with the <link> it doesn't give an error but I need <a>.
          MyComponent
      </a>
  );
};
3
  • 1
    Have a look at: developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement Commented May 27, 2020 at 9:05
  • 1
    I'm afraid it's not clear what you're asking here. Maybe you could get a colleague to help you write the question? But regarding "I could not see an 'HTMLAElement' structure": The type of an a element is HTMLAnchorElement. You can find those types in this list in the HTML5 specification. Commented May 27, 2020 at 9:05
  • Thank you 🙏 🙏 🙏 . I did not know that the element 'a' corresponds to 'anchor'. my shame 🤦🏻 Commented May 27, 2020 at 9:10

4 Answers 4

13

My problem is solved. Errors disappeared when I used 'HTMLAnchorElement'. I did not know that the element 'a' corresponds to 'anchor'.

Sign up to request clarification or add additional context in comments.

Comments

5

You're trying to use an <a> tag but you're telling TypeScript it should expect props for a <link> tag. Those are completely different HTML elements.

  • the props don't match up! For an <a> tag (anchor/link that you click), you need to use HTMLAnchorElement instead. Here's how you'd fix it:
const MyComponent: React.FC<React.AnchorHTMLAttributes<HTMLAnchorElement>> = (
  props
) => {
  return (
    <a {...props} className="mycomponent">
      MyComponent
    </a>
  );
};

1 Comment

I usually cringe when I see a new answer to an old question because they're almost uniformly bad, but this was a pleasant surprise of an exception. Nice!
2

Using AnchorHTMLAttributes<HTMLAnchorElement>.

Example:

import { AnchorHTMLAttributes, FC } from 'react';

interface Props extends AnchorHTMLAttributes<HTMLAnchorElement> {
  className?: string;
  title: string;
}

const OpenNewTab: FC<Props> = ({ className, title, ...props }) => {
 <a target="_blank" className={className} {...props}>
     <span>{title}</span>
 </a>
}

Comments

0

To further ellaborate on Samuel Tosin's response,

If you set up a new project using npm create vite@latest (React 19, but should work with React 17 and 18), you can create the following function component:

import React from "react";

export type CustomAnchorProps = React.AnchorHTMLAttributes<HTMLAnchorElement> & {
  external?: boolean;
};

const CustomAnchor: React.FC<CustomAnchorProps> = ({
  children,
  className = "",
  external = false,
  target,
  rel,
  ...rest
}) => {
  return (
    <a
      {...rest}
      target={external ? "_blank" : target}
      rel={external ? "noopener noreferrer" : rel}
      className={`CustomAnchor ${className}`}
    >
      {children}
    </a>
  );
};

export default CustomAnchor;

The component supports the base anchor attributes, plus an additiona ones defined in the CustomAnchorProps type union. Additionally, the children prop allows you to use this component to wrap anything you want.

Here is the usage (I just blew-away the default App.tsx in the Vite setup):

import CustomAnchor from "./components/CustomAnchor";

import "./App.css";

function App() {
  return (
    <>
      <div>
        <CustomAnchor href="https://example.com" external>
          <h2>External Link</h2>
        </CustomAnchor>

        <CustomAnchor href="/about">
          <h2>Internal Link</h2>
        </CustomAnchor>
      </div>
    </>
  );
}

export default App;

Comments

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.