reactjs 使用来自各种接口的 prop ,而不显式命名它们

gopyfrb3  于 2023-01-25  发布在  React
关注(0)|答案(2)|浏览(122)

假设我有这个

interface ButtonProps {
    children?: React.ReactNode,
    backgroundColor?: Colors,
    style?: object,
    disabled?: boolean,
    classes?: string
    onClick?: () => void
}

const Button = ({
                        backgroundColor,
                        classes,
                        children,
                        disabled = false,
                        onClick,
                        style,
                    }: ButtonProps) => {

    const _constructInlineStyles = (): CSSProperties => {
        ...
    }

    return (
        <button
            className={clsx(classes, 'button-container')}
            disabled={disabled}
            onClick={onClick}
            style={...}>
                {children}
        </button>
    )
}

export default Button;

然后,我想创建一个TextButton,它使用Button之上的构建。

interface TextButtonProps {
        children: string;
    }
    
    const TextButton = ({
                            children,
                        }: TextButtonProps) => {
        return (
            <Button>
                <Text>
                    {children}
                </Text>
            </Button>
        )
    }

export default TextButton;

我们可以这样写吗,ButtonPropsTextButton可用,而不命名它们。

interface TextButtonProps {
        children: string;
    }
    
    const TextButton = ({
                            children,
                            backgroundColor,
                            classes,
                            disabled = false,
                            onClick,
                            style,
                        }: TextButtonProps & ButtonProps ) => {
        return (
            <Button
                 className={classes}
                 disabled={disabled}
                 onClick={onClick}
                 style={style}>
                <Text>
                    {children}
                </Text>
            </Button>
        )
    }

就像

interface TextButtonProps {
        children: string;
        props: ButtonProps;
    }
    
    const TextButton = ({
                            children,
                            props,
                        }: TextButtonProps) => {
        return (
            <Button
                 {...props}>
                <Text>
                    {children}
                </Text>
            </Button>
        )
    }

这样我就可以

<TextButton 
    disabled={true} >
    Fooo
</TextButton>

sgtfey8w

sgtfey8w1#

可以使用spread运算符接收函数调用中的其余props,并将其传递给Button组件。要使用正确的子类型,请使用Omit参考this答案:

import { Text} from 'react-native-paper'
interface ButtonProps {
  disabled?: boolean;
  onClick?: () => void;
  children: JSX.Element;
}

const Button = (props: ButtonProps) => {
  return <div>{props.children}</div>;
};

interface TextButtonProps extends Omit<ButtonProps, "children"> {
  children: string;
}

const TextButton = ({ children, ...props }: TextButtonProps) => {
  return (
    <Button {...props}>
      <Text>{children}</Text>
    </Button>
  );
};

const example = <TextButton disabled>Hi</TextButton>;
0aydgbwb

0aydgbwb2#

您可以extend interfaces,而omitting是一个属性,如下所示。

interface TextButtonProps extends Omit<ButtonProps, 'children'> {
        children: string;
}

相关问题