React Reference ยท Built-in DOM Components
The React a element
Reference for the React anchor element: set href, open new tabs safely with rel, intercept clicks for client-side routing and avoid javascript: URLs.
<a> is the hyperlink. React adds nothing special to it: a plain <a href="/pricing"> causes a normal browser navigation and a full page load. What React gives you is onClick, so you can intercept a click, update state or a router and call event.preventDefault() to stop the page load. Routing libraries wrap exactly this pattern in a <Link> component.
Use <a> whenever the action takes the user somewhere (another page, a section, a file). If the action does something on the current page, a <button> is the right element.
<a href="https://example.com/docs" target="_blank" rel="noopener noreferrer"> Docs </a>
Try it
Syntax
<a href="url" target="_blank" rel="noopener noreferrer" onClick={handler}> Link text </a>
| Name | Description |
|---|---|
href |
The destination: an absolute or relative URL, a #fragment, mailto: or tel:. Without href the element is a placeholder, not a link. |
target |
Where to open it: _self (default), _blank (new tab), _parent, _top. |
rel |
Relationship. Use noopener noreferrer for links to other sites in new tabs; nofollow, ugc or sponsored for SEO hints. |
download |
Suggests downloading instead of navigating; the value is a file name. Same-origin URLs only. |
hrefLang, type, referrerPolicy, ping |
Hints about the target. Note the camelCase hrefLang and referrerPolicy. |
onClick |
Click handler. Call preventDefault() to stop the navigation. |
Returns: an HTMLAnchorElement.
Edge cases: React 19 does not allow javascript: URLs in href. It replaces the value with a URL that throws an error saying React has blocked it, so use a button with onClick instead.
Usage notes
- Internal and external links differ. For a link to the same site inside a single-page app, a click handler or a router
Linkavoids a page reload. For other sites, a normal<a href>is correct. - Keep
hrefreal. Even with a click handler, give the anchor a validhref. Middle click, Ctrl or Cmd click, "Open in new tab", crawlers and screen readers all depend on it. Never usehref="#"with a handler just to get a pointer cursor; use a button. - New tabs and security. Modern browsers treat
target="_blank"asnoopenerautomatically, but writingrel="noopener noreferrer"is still a good habit andnoreferreralso hides the referrer. - User-supplied URLs. If
hrefcomes from user input, check the scheme (allowhttp:,https:,mailto:) before rendering it. - Current page. Mark the active link with
aria-current="page". Screen readers announce it, and CSS can style[aria-current="page"]. - Link text. Write text that makes sense out of context. "Read the pricing details" beats "Click here".
- Do not nest. An
<a>inside another<a>is invalid, and React warns about it. A button inside an<a>is also invalid HTML (interactive content inside interactive content), even though React does not warn. - Prefetching. Plain anchors are not prefetched. Frameworks add that on top by observing the link in the viewport.
Related
<button>,<link>,<img>- Lessons: Client-side routing, Handling events