带有 TS 的 makeStyles 中的主题和道具

时间:2021-05-12 05:54:46

标签: reactjs material-ui makestyles

发现了很多类似的问题,但没有一个能完全回答这个问题。您如何将 themeprops 传递给 Material UI makeStyles 钩子,但从最外层的闭包访问它们而不会发生 TS 爆炸?像这样:

type StyleProps = {
  post: Post;
}

const useStyles = makeStyles<Theme, StyleProps>((theme: Theme, props: StyleProps) => ({
    root: {
      maxWidth: '100%',
      backgroundImage: ({ post }) => post.mainImage
    },
    date: {
      margin: theme.spacing(1),
      marginLeft: theme.spacing(2)
    },
    heroimage: {
      maxWidth: '100%',
      height: 'auto',
      objectFit: 'cover'
    }
}))

TS 错误:

Type '(theme: Theme, props: any) => { root: (props: any) => CSSProperties; expandable: (props: any) => CSSProperties; iconContainer: { position: string; top: number; right: number; display: string; }; expandMoreIcon: (props: any) => CSSProperties; bannerContent: (props: any) => CSSProperties; }' is not assignable to type 'StyleRulesCallback<Theme, StyleProps, string>'.

我可以像您在 root 中看到的那样从样式块内联访问 props,但这不是我要找的。

0 个答案:

没有答案