Home > other >  React Ref Object HTMLAElement
React Ref Object HTMLAElement

Time:11-09

I can do any of:

React.RefObject<HTMLDivElement>
React.RefObject<HTMLInputElement>
React.RefObject<HTMLSpanElement>

But it doesn't work to do:

React.RefObject<HTMLAElement>

For <a> tags.

It seems they choose not to do a logical naming convention?

Cannot find any documentation which lists the conversion between 'HTML tag' and 'how react arbitrarily decided to call it'.

CodePudding user response:

You can easily get the type of a element as follows (type in your browser console, not in a script):

const t = document.createElement('a');
console.log(t.__proto__);

As you can see a <a> element will be a HTMLAnchorElement. This is built into HTML and is not react/typescript specific. Replace 'a' with any other element to get it's type.

CodePudding user response:

The a in the <a></a> element is stand for Anchor. So if you want to use the anchor element type you can simply use the HTMLAnchorElement.

According to the MDN documentation:

The HTMLAnchorElement interface represents hyperlink elements and provides special properties and methods (beyond those of the regular HTMLElement object interface that they inherit from) for manipulating the layout and presentation of such elements. This interface corresponds to element

You can also see the list of HTML types on the MDN web API's page, in H column.

  • Related