小编M.G*_*AKA的帖子

从 props 更改自定义按钮的类型

我在 React 中创建了一个自定义按钮组件,但无法更改它的类型。我尝试将类型更改为,String但打字稿给我一个错误,指出它不可分配。任何帮助,将不胜感激。

import React from "react";

interface Props {
  children?: React.ReactNode;
  className: string;
  onClick: () => void;
  type?: string;   ---> I tried making this string 
}

const STYLES = ["bg-gradient-to-r from from-gold to to-gold-dark","bg-gradient-to-r from from-primary to to-primary-dark"];
const BUTTONTYPE = ["submit","reset"];

//created a array to change type according to input
const CustomButton = (props: Props) => {
  const checkButtonStyle = STYLES.includes(props.className)
    ? props.className
    : "NULL";

  const checkButtonType = BUTTONTYPE.includes(props.type) ? props.type : "button";  
  return (
//gives …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

2
推荐指数
1
解决办法
2676
查看次数

标签 统计

reactjs ×1

typescript ×1