[英]Pass props to makeStyles and use in CSS shorthand property in Material UI
I pass props to a button component: 我将道具传递给按钮组件:
const StoreButton = ({ storeColor }) => {
const borderBottom = `solid 3px ${storeColor}`;
const classes = useStyles({ borderBottom });
return (
<Button variant="outlined" size="small" className={classes.button}>
Store
</ Button>
)
};
I create the borderBottom
property before I use it in classes. 我先在类中使用
borderBottom
属性,然后再使用它。 I would like to construct the property inside makeStyles but this leads to an error: 我想在makeStyles内部构造属性,但这会导致错误:
const useStyles = makeStyles(theme => ({
button: {
borderBottom: props => props.borderBottom,
// borderBottom: `solid 3px ${props => props.borderBottom}`; // function is pasted in
},
}));
If I construct the CSS shorthand inside makeStyles it creates solid 3px solid 3px function(props){return props.borderBottom;}
. 如果我在makeStyles中构造CSS速记,则会创建
solid 3px solid 3px function(props){return props.borderBottom;}
。 when I inspect it in Chrome. 当我在Chrome中检查它时。 Thus, this way the styles are invalid.
因此,这种方式无效。
Is there a way to pass props to CSS shorthand properties without this workaround of creating it outside makeStyles? 有没有一种方法可以将prop传递给CSS速记属性,而无需在makeStyles之外创建它的解决方法?
You almost had it right, the only thing to change is this: 您几乎是对的,唯一要更改的是:
borderBottom: props => `1px solid ${props.color}`,
Now the value is a proper function. 现在,该值是一个适当的函数。 Inside the function it constructs the correct string value.
在函数内部,它构造正确的字符串值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.