在内部实现"makeStyles".在React Material-UI中? [英] Internal implementation of "makeStyles" in React Material-UI?

查看:270
本文介绍了在内部实现"makeStyles".在React Material-UI中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的目的是了解幕后发生的事情.每次使用makeStyles()找到代码时,我都会感觉自己只是在进行纯复制粘贴,而不了解引擎盖下发生的事情.所以我想在这里发布一个问题,以便有人可以帮助我.

The purpose of this question is to understand what is happening under the hood. Every time I found the code with makeStyles() I feel that I am just doing a pure copy paste without understanding what is happening under the hood. So I thought of posting a question here so that some one can help me out.

我已经在许多React应用程序中看到以下类型的代码.我很想知道当我们调用makeStyles()时发生了什么.因此,我跳入了它的定义,但我无法理解它的含义.有人可以帮助我了解如何阅读/理解它.

I have seen the below kind of code in many React apps. I was so curious to know what is happening when we make a call to makeStyles(). So I jumped into its definition but I am not able to understand what does it mean. Can someone help me understand how to read/understand it.

我在这里理解的是我正在传递一个名为theme的参数的函数.通过该函数后,我不知道发生了什么.我也很高兴有人能帮助我解决这个问题.

What I understand here is that I am passing a function with parameter named theme. After passing that function I have no clue what is going on. I appreciate if some one help me figure out this as well.

// 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>>;

推荐答案

输入/输出/副作用的高级视图

makeStyles

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆