首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在React中使用“类”支柱时的混淆

在React中使用“类”支柱时的混淆
EN

Stack Overflow用户
提问于 2022-09-13 09:25:08
回答 1查看 39关注 0票数 0

我正在学习反应和MUI。我试图按照给定的这里步骤创建一个侧栏。指南使用与makeStyles不兼容的React18。所以,我尝试了以下几种方法:

代码语言:javascript
复制
    import * as React from "react";
    import Box from "@mui/material/Box";
    import CssBaseline from "@mui/material/CssBaseline";
    import Drawer from "@mui/material/Drawer";
    import Typography from "@mui/material/Typography";
    import { useTheme } from "@mui/material/styles";
    
    const drawerWidth = 240;
    
    const SideBar = () => {
      const theme = useTheme();
    
      const drawerPaper = {
        position: "relative",
        backgroundColor: "#535454",
        color: "#fff",
        whiteSpace: "nowrap",
        paddingTop: theme.spacing(4),
        paddingBottom: theme.spacing(4),
        width: drawerWidth,
      };
    
    return (
        <Box className="root">
          <CssBaseline />
          <Drawer variant="permanent" classes={{ paper: drawerPaper }}>
            <Typography>I am a SideBar</Typography>
          </Drawer>
        </Box>
      );
    };
    
    export default SideBar;

这不管用。css没有出现。有人能告诉我怎么做吗?我也找不到很多关于如何使用道具类的文档。与文件的链接也会有所帮助。

编辑:

我已经按照@Ippizinidev和@Hamed的建议尝试使用sx。那不管用。使用sx,我得到了结果。但我想要像这样的东西。在第二个例子中,我将css放在一个单独的.css文件中。但我不能从这种方法中使用useTheme

EN

回答 1

Stack Overflow用户

发布于 2022-09-13 10:01:11

不用使用makeStyles(),您可以使用useStyles()

有关如何使用此方法,请参见这个问题

但在您的情况下,最好使用sx属性。所以你的代码是:

代码语言:javascript
复制
<Drawer variant="permanent" sx={drawerPaper}>
  <Typography>I am a SideBar</Typography>
</Drawer>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73700537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档