Skip to content

Creating components with class as an argument? #4059

Answered by rschristian
kurtbuilds asked this question in Q&A
Discussion options

You must be logged in to vote

I'm not quite sure I follow, are you asking about types (your commented out extends) or using class as a prop? If the former, see the code sample below, if the latter, everything works the same as it would in React. Obviously, you can't destructure class like that, it being a reserved word and all in JS. You can use any other name, avoid destructuring, destructure but assign to another name, etc., however.

import type { JSX } from 'preact';

interface ButtonProps extends JSX.HTMLAttributes<HTMLButtonElement> {
  type: 'rounded' | 'square';
}

function MyButton(props: ButtonProps) {
  const btnStyle =
    props.type === 'rounded' ? 'btn btn-rounded' : 'btn btn-square';

  return <button class

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@kurtbuilds
Comment options

@rschristian
Comment options

@developit
Comment options

Answer selected by kurtbuilds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants