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.

Cannot use custom component props with Button and typescript

See original GitHub issue

Describe the issue. What is the expected and unexpected behavior? Button allows one to specify the component prop. The idea being that you can switch from rendering a button to something else such as an anchor tag. This works fine if the component is an html element, but not when it is a custom React component.

I want to create a button backed by a react-router Link however in doing so the Button component will not accept the to property.

Ideally I should be able to do parameterize the component such as:

<Button<React.ComponentProps<typeof Link>> component={Link} to="...">my link</Button>

Please provide the steps to reproduce. Feel free to link CodeSandbox or another tool.

The following results in a typescript error for attribute to:

<Button component={Link} to="...">my link</Button>

Is this a bug or enhancement? If this issue is a bug, is this issue blocking you or is there a work-around? bug

What is your product and what release version are you targeting? OpenShift 4.8

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
tlabajcommented, Jul 21, 2021

@mcarrano can we pull this into the next milestone.

0reactions
jonkoopscommented, Jul 21, 2021

Note that this same code works perfectly fine with the types from styled-components (see below), perhaps the types from there could be borrowed.

import React from "react";
import { Link } from "react-router-dom";
import styled from "styled-components";

const Button = styled.button`
  background-color: hotpink;
`;

export const MyLinkButton = () => <Button as={Link} to="/some-path"></Button>;
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript React, unable to send props to component
I have a simple button component, I am ...
Read more >
Making your components extensible with TypeScript
Great, we've made use of TypeScript's intersection types to combine the native button props with our custom props. We only got one problem....
Read more >
How do I provide props to styled elements in Typescript? #630
Use styled-components to change a style based on a prop that I pass down. Here is how I solved it: import React from...
Read more >
Useful Patterns by Use Case - React TypeScript Cheatsheets
Usecase: you want to make a <Button> that takes all the normal props of <button> ... but for a React Component you don't...
Read more >
TypeScript Tips: Getting Component Props Types in React
Let's talk about the props types. When you define your HoC component, its props should have the exact type of the returning component....
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