我将Material-UI
中的示例用于AppBar
,并将其从函数更改为类组件,然后查看了如何使用withStyles
并做了完全一样,但是无论我做什么,我所做的任何改变都不会应用样式
"react": "^16.8.6",
"@material-ui/core": "^4.1.2",
"@material-ui/icons": "^4.2.1",
import React, {Component} from 'react';
import styleClasses from './SideDrawer.module.css';
import { withStyles } from '@material-ui/styles';
import PropTypes from 'prop-types';
// import UIManager from '../../UIManager/UIManager';
import Drawer from '@material-ui/core/Drawer';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import { fade, makeStyles } from '@material-ui/core/styles';
import IconButton from '@material-ui/core/IconButton';
import Typography from '@material-ui/core/Typography';
import InputBase from '@material-ui/core/InputBase';
import Badge from '@material-ui/core/Badge';
import MenuItem from '@material-ui/core/MenuItem';
import Menu from '@material-ui/core/Menu';
import MenuIcon from '@material-ui/icons/Menu';
import SearchIcon from '@material-ui/icons/Search';
import AccountCircle from '@material-ui/icons/AccountCircle';
import MailIcon from '@material-ui/icons/Mail';
import NotificationsIcon from '@material-ui/icons/Notifications';
import MoreIcon from '@material-ui/icons/MoreVert';
const useStyles = makeStyles(theme => ({
grow: {
flexGrow: 1,
zIndex: 1000,
},
menuButton: {
marginRight: theme.spacing(2),
},
title: {
display: 'none',
[theme.breakpoints.up('sm')]: {
display: 'block',
},
color: 'red'
},
search: {
position: 'relative',
borderRadius: theme.shape.borderRadius,
backgroundColor: fade(theme.palette.common.white, 0.15),
'&:hover': {
backgroundColor: fade(theme.palette.common.white, 0.25),
},
marginRight: theme.spacing(2),
marginLeft: 0,
width: '100%',
[theme.breakpoints.up('sm')]: {
marginLeft: theme.spacing(3),
width: 'auto',
},
},
searchIcon: {
width: theme.spacing(7),
height: '100%',
position: 'absolute',
pointerEvents: 'none',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
inputRoot: {
color: 'inherit',
},
inputInput: {
padding: theme.spacing(1, 1, 1, 7),
transition: theme.transitions.create('width'),
width: '100%',
[theme.breakpoints.up('md')]: {
width: 200,
},
}
}));
class SideDrawer extends Component {
render () {
const { classes } = this.props;
console.log('classes', classes)
return (
<div className={styleClasses.grow}>
<AppBar className={'SideDrawer-inputInput-14'}>
<Toolbar>
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="Open drawer"
>
<MenuIcon />
</IconButton>
<Typography className={classes.title} variant="h6" noWrap>
Test
</Typography>
<div className={classes.search}>
<div className={classes.searchIcon}>
<SearchIcon />
</div>
<InputBase
placeholder="search..."
classes={{
root: classes.inputRoot,
input: classes.inputInput,
}}
inputProps={{ 'aria-label': 'Search' }}
/>
</div>
<div className={classes.grow} />
</Toolbar>
</AppBar>
</div>
)
}
}
SideDrawer.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(useStyles)(SideDrawer);
console.log('classes', classes)
返回:
{grow: "SideDrawer-grow-8", menuButton: "SideDrawer-menuButton-9", title: "SideDrawer-title-10", search: "SideDrawer-search-11", searchIcon: "SideDrawer-searchIcon-12", …}
grow: "SideDrawer-grow-8"
inputInput: "SideDrawer-inputInput-14"
inputRoot: "SideDrawer-inputRoot-13"
menuButton: "SideDrawer-menuButton-9"
search: "SideDrawer-search-11"
searchIcon: "SideDrawer-searchIcon-12"
title: "SideDrawer-title-10"
但是这些样式中的任何一个都不应用于实际项目或AppBar
,我在做什么错了?
答案 0 :(得分:1)
您不应该尝试将makeStyles
与withStyles
一起使用。 makeStyles
返回一个自定义钩子,并将此自定义钩子传递到withStyles
中将无法正常工作。
相反,您需要以下内容:
const styles = theme => ({
grow: {
flexGrow: 1,
zIndex: 1000,
},
/* and all your other styles ... */
});
// other stuff (e.g. your SideDrawer component) ...
export default withStyles(styles)(SideDrawer);