Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React component props with hyphen naming

I have a component Button in react with below interface

interface ButtonI extends React.HTMLAttributes<HTMLButtonElement> {
  background?: string;
  border?: string;
  children?: React.ReactElement;
  className?: string;
  color?: string;
  disabled?: boolean;
  onClick?: (evt: any) => void;
  size?: 'small' | 'medium' | 'large' ;
}

I want to pass data-test as a prop, but I get error from typescript if I define data-test in interface as

data-test?:string;

Can someone tell me how can I pass props in react with hyphen in names?

like image 485
Varun Sukheja Avatar asked May 08 '26 03:05

Varun Sukheja


1 Answers

I found the solution as below:
In the interface I used single quotes

'data-test'?: string;

and while destructuring I aliased it as

const {
      background,
      border,
      children,
      className,
      color,
      disabled,
      onClick,
      size,
      'data-test':dataTest
    } = props;
like image 76
Varun Sukheja Avatar answered May 09 '26 16:05

Varun Sukheja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!