MUI样式的组件和TypeScript

ttygqcqt  于 2022-11-18  发布在  TypeScript
关注(0)|答案(2)|浏览(265)

我有以下代码

import {Button,ButtonProps} from '@mui/material';
import { alpha, styled } from '@mui/material/styles';
import { Link as RouterLink } from 'react-router-dom';

const BackButton = styled(Button)<ButtonProps>(({ theme }) => ({
  borderColor: alpha(theme.palette.text.primary, 0.5),
  padding: theme.spacing(1),
}));

const backButton = (
    <BackButton
      to={'/my/path'}
      component={RouterLink}
      fullWidth
      variant="outlined"
      color="inherit"
    >
      Back
    </BackButton>
  );

我有一个to道具的打字错误,我该如何修复它?

TS2322: Type '{ children: string; to: string; component: typeof Link; fullWidth: true; variant: "outlined"; color: "inherit"; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; classes?: Partial<ButtonClasses>; color?: "inherit" | "error" | "primary" | "secondary" | "success" | "info" | "warning"; ... 9 more ...; variant?: "text" | ... 1 more ... | "contained"; } & ... 4 more ... & { ...; }'.   Property 'to' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; classes?: Partial<ButtonClasses>; color?: "inherit" | "error" | "primary" | "secondary" | "success" | "info" | "warning"; ... 9 more ...; variant?: "text" | ... 1 more ... | "contained"; } & ... 4 more ... & { ...; }'.
laawzig2

laawzig21#

请参阅MUI Typescript页面上的组件复杂功能部分。
修改已设置样式的构件,以在末端添加类型转换:

const BackButton = styled(Button)(({ theme }) => ({
  borderColor: alpha(theme.palette.text.primary, 0.5),
  padding: theme.spacing(1),
})) as typeof Button;
lsmepo6l

lsmepo6l2#

尝试向按钮添加onClick处理程序以处理重定向:

const handleClick = () => {
   navigate('/my/path')
}

const backButton = (
    <BackButton
      onClick={handleClick}
      component={RouterLink}
      fullWidth
      variant="outlined"
      color="inherit"
    >
      Back
    </BackButton>
  );

相关问题