微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决带有 TS 的 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,但这不是我要找的。​​p>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。