【发布时间】:2021-04-11 04:46:35
【问题描述】:
我在 Material UI 中有一个输入表单开关,我希望轨道和按钮在打开时显示为红色,就像在 this example 中一样。 Material UI 网站上的所有switch styling examples 都使用 withStyles,但我不确定为什么不能使用 useStyles 代替?是否可以使用 useStyles 做到这一点?
这是我的尝试,但不起作用!
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Switch from '@material-ui/core/Switch';
import FormGroup from '@material-ui/core/FormGroup';
import FormControlLabel from '@material-ui/core/FormControlLabel';
const useStyles = makeStyles(theme => ({
switchBase: {
'&$checked + $track': {
backgroundColor: 'red',
},
},
}));
export default function SwitchesSize() {
const classes = useStyles()
const [checked, setChecked] = React.useState(false);
const toggleChecked = () => {
setChecked((prev) => !prev);
};
return (
<FormGroup>
<FormControlLabel
control={<Switch size="small" checked={checked} onChange={toggleChecked} />}
label="Small"
classes={{ switchBase: classes.switchBase }}
/>
</FormGroup>
);
}
非常感谢您的帮助:)
凯蒂
【问题讨论】:
标签: css reactjs material-ui