繁体   English   中英

如何在 react/typescript 中定义按钮的类型?

[英]How to define the type of a button in react / typescript?

我目前正在尝试定义按钮组件的类型,它当前设置为 any 所以它正在工作,但是这个属性的真正类型是什么?

import React, { Dispatch, SetStateAction } from "react";

interface ButtonI {
  className?: string;
  label?: string;
  children: React.ReactNode;
  type?: any;
  onClick?: Dispatch<SetStateAction<boolean>>;
}

const Button: React.FC<ButtonI> = (props) => {
  return (
    <button
      className={`${props.className}`}
      type={props.type || "button"}
      aria-label={props.label || ""}
      onClick={() => (props.onClick ? props.onClick(true) : null)}
    >
      {props.children}
    </button>
  );
};

export default React.memo(Button);

按钮类型的类型是

React.ButtonHTMLAttributes<HTMLButtonElement>["type"]

用法:

type?: React.ButtonHTMLAttributes<HTMLButtonElement>["type"]

它接受:

"button" | "reset" | "submit" | undefined

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM