[英]Material-UI styles: convert functional component to class component
So I try the following code to convert a functional component to the classical component, it kinda worked, no error but styles are not applied .所以我尝试使用以下代码将功能组件转换为经典组件,它有点工作,没有错误但没有应用样式。
import { makeStyles } from '@material-ui/core/styles';
import { withStyles } from '@material-ui/core/styles';
const playtheMusic = () => {
pauseMusic();
};
const pausetheMusic = () => {
pauseMusic();
};
const useStyles = makeStyles(theme => ({
text: {
padding: 50
},
paper: {
paddingBottom: 50
},
list: {
marginBottom: theme.spacing(2)
},
subheader: {
backgroundColor: theme.palette.background.paper
},
appBar: {
top: 'auto',
bottom: 0,
backgroundColor: '#282828',
padding: '15px'
},
grow: {
flexGrow: 1
}
}));
class BottomAppBar extends React.Component {
// const classes = useStyles();
render() {
const { classes } = this.props;
return (
<div>
<AppBar position="fixed" className={classes.appBar}>
<div style={{ display: 'flex', alignItems: 'center', alignContent: 'center' }}>
<div>
<Typography style={{ fontSize: 15 }}> Stress Out </Typography>
<br />
<Typography style={{ fontSize: 12, color: '#B3B3B3' }}>
Twenty One Pilots
</Typography>
</div>
<div className={classes.grow} />
<div>
<IconButton style={{ color: 'white' }}>
<ShuffleIcon />
</IconButton>
<IconButton style={{ color: 'white' }}>
<SkipPreviousRoundedIcon style={{ fontSize: 30 }} />
</IconButton>
<IconButton onClick={pausetheMusic} style={{ color: 'white' }}>
<PauseCircleOutlineRoundedIcon style={{ fontSize: 46 }} />
<PlayCircleOutlineIcon style={{ fontSize: 46, display: 'none' }} />
</IconButton>
<IconButton style={{ color: 'white' }}>
<SkipNextRoundedIcon style={{ fontSize: 30 }} />
</IconButton>
<IconButton style={{ color: 'white' }}>
<RepeatIcon />
</IconButton>
</div>
<div className={classes.grow} />
<div>
<IconButton style={{ color: 'white' }}>
<VolumeUpIcon />
</IconButton>
</div>
</div>
</AppBar>
</div>
);
}
}
export default withStyles(useStyles)(BottomAppBar);
also, there is a problem with StackOverflow.
此外,StackOverflow 也存在问题。 it says "It looks like your post is mostly code; please add some more details".
它说“看起来您的帖子主要是代码;请添加更多详细信息”。 that's the reason I'm writing some unnecessary things XD you can skip it.
这就是我写一些不必要的东西的原因 XD 你可以跳过它。
Thanks for reading.谢谢阅读。 have a good day <3
祝你有美好的一天 <3
A common approach for material-ui component styling : material-ui 组件样式的常用方法:
withStyles (High order function) + createStyles withStyles(高阶函数)+ createStyles
useStyles (hooks) + makeStyles useStyles(钩子)+ makeStyles
In your code, you shall not use the hooks useStyles
inside withStyle
, hooks shouldn't be used inside any classical component
,在代码中,你不得使用挂钩
useStyles
内withStyle
,钩不应该任何内部使用classical component
,
export default withStyles(useStyles)(BottomAppBar);
import { withStyles, createStyles } from "@material-ui/core/styles";
const styles = theme => createStyles({
root: {
},
// ...
});
...
const { classes } = this.props;
...
export default withStyles(styles)(App);
Online sample for both classical component
and functional component
styling classical component
和functional component
样式的在线示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.