question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Button] href does not exist when used as anchor

See original GitHub issue

Describe the bug A clear and concise description of what the bug is.

I’m using typescript. When using the Button component with as="a" prop, it’s throwing type error. My usage looks like:

<Button as="a" href="http://localhost:4000/auth/github" leftIcon={FiGithub}>
  Login with GitHub
</Button>

The error being thrown is:

Type '{ children: string; as: "a"; href: string; leftIcon: IconType; }' is not assignable to type 'IntrinsicAttributes & IButton & Pick<PseudoBoxProps, "fontWeight" | "lineHeight" | "fontSize" | "letterSpacing" | "fontFamily" | ... 436 more ... | "_placeholder"> & { ...; }'.
 |   Property 'href' does not exist on type 'IntrinsicAttributes & IButton & Pick<PseudoBoxProps, "fontWeight" | "lineHeight" | "fontSize" | "letterSpacing" | "fontFamily" | ... 436 more ... | "_placeholder"> & { ...; }'.

I suspect this might be an issue with type definitions of Button where it would need to support props of a if the component is being rendered as an anchor tag.

Expected Behavior A clear and concise description of what you expected to happen.

The error should not happen when as="a" is passed but it is valid if the Button is being rendered as an actual button

To Reproduce

  1. Use a Button component in a TS file with the above usage (with as="a" and href prop)
  2. Check TS errors

Suggested solution(s)

I think type definitions have to be updated for this. Not sure how they should be handled dynamically based on as prop at the moment.

Desktop (please complete the following information):

  • OS: MacOS
  • Browser Firefox 71.0
  • Version 0.52.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
corysimmonscommented, May 15, 2021

Posting this here because it’s the first result for linkbutton chakra.

For NextJS users, I use this and it seems to work as expected:

import Link, { LinkProps } from 'next/link'
import { Button, ButtonProps } from '@chakra-ui/react'

type Props = ButtonProps & LinkProps;

const LinkButton = ({ href, children, ...props }: Props) => (
	<Link href={href}>
		<Button as="a" cursor="pointer" {...props}>{children}</Button>
	</Link>
)

export default LinkButton
1reaction
segunadebayocommented, Jan 31, 2020

HI @divyanshu013, this has been resolved in the current TypeScript version we’re working on. I’ll add this to #205 for safe-keeping and double-checking.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Valid to use <a> (anchor tag) without href attribute? [duplicate]
Yes, it is valid to use the anchor tag without a href attribute. If the ...
Read more >
Useful Patterns by Use Case - React TypeScript Cheatsheets
A very common use case for this is to render something as either a button or an anchor, based on if it receives...
Read more >
How to Dynamically Render a Button or Link in Drupal or Gatsby
It is not uncommon for links to be styled like buttons when ... The href attribute will always be present when using an...
Read more >
Creating anchor links - Squarespace Help Center
An anchor link (or "page jump") is a special URL that takes you to a specific place on a page. For example, the...
Read more >
A Complete Guide to Links and Buttons - CSS-Tricks
Buttons are for triggering actions. When do you use the <button> element? A good rule is to use a button when there is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found