这个问题的目的是了解幕后发生的事情。每次我找到代码时,makeStyles()我都觉得我只是在做一个纯粹的复制粘贴,而不了解幕后发生的事情。所以我想在这里发布一个问题,以便有人可以帮助我。
我在许多 React 应用程序中看到过以下类型的代码。当我们打电话给 时,我很想知道发生了什么makeStyles()。所以我跳入了它的定义,但我无法理解它的含义。有人可以帮助我理解如何阅读/理解它。
我在这里理解的是,我正在传递一个名为theme. 通过该函数后,我不知道发生了什么。如果有人也帮我弄清楚这一点,我将不胜感激。
// Calling makeStyles()
const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
},
drawer: {
[theme.breakpoints.up('sm')]: {
width: drawerWidth,
flexShrink: 0,
},
},
appBar: {
marginLeft: drawerWidth,
[theme.breakpoints.up('sm')]: {
width: `calc(100% - ${drawerWidth}px)`,
},
},
menuButton: {
marginRight: theme.spacing(2),
[theme.breakpoints.up('sm')]: {
display: 'none',
},
},
toolbar: theme.mixins.toolbar,
drawerPaper: {
width: drawerWidth,
},
content: {
flexGrow: 1,
padding: theme.spacing(3),
},
}));
//definition of makeStyles()
import { Theme as DefaultTheme } from './createMuiTheme';
import { Styles, WithStylesOptions } from '@material-ui/styles/withStyles';
import { StylesHook } from '@material-ui/styles/makeStyles';
export default function makeStyles<
Theme = DefaultTheme,
Props extends {} = {},
ClassKey extends string = string
>(
styles: Styles<Theme, Props, ClassKey>,
options?: WithStylesOptions<Theme>,
): StylesHook<Styles<Theme, Props, ClassKey>>;