简体   繁体   English

是否可以将道具传递给 mui styled() 组件?

[英]Is it possible to pass props to mui styled() component?

Is it possible to pass default props to a style component?是否可以将默认道具传递给样式组件? For example, I want to make a component with using Typography from mui.例如,我想使用 mui 的 Typography 制作一个组件。

const H1Typography = (props) => (
  <Typography variant='h1' sx={{fontWeight: 500}}>{props.children}</Typography>
)

. . How can I add variant props into the below mui styled component?如何将变体道具添加到下面的 mui 样式组件中?

const H1Typography = styled(Typography)(({theme}) => ({
  fontWeight: 500
}))

https://mui.com/system/styled/ https://mui.com/system/styled/

You can pass it like you pass theme, but if you use a boolean prop you will receive an error.您可以像传递主题一样传递它,但是如果您使用 boolean 道具,您将收到错误消息。

const H1Typography = styled(Typography)(({theme, huge}) => ({fontWeight: 500,fontSize: huge === "true" ? "4rem" : "1rem"}))

Note, that I pass the huge prop as a string.请注意,我将巨大的道具作为字符串传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM