简体   繁体   English

我们可以在 _app.js 中同时使用 MUI makeStyles 和 Theming

[英]Can we use both MUI makeStyles and Theming in _app.js

I'm making a tricky nav component using MUI Persistent Drawers with a React + Next structure.我正在使用具有 React + Next 结构的MUI 持久抽屉制作一个棘手的导航组件。 In order to get the content shrinking thanks to the state change, I had to put the whole navigation system inside of the _app.js file instead of having it in a distinct component.由于 state 更改,为了缩小内容,我不得不将整个导航系统放在 _app.js 文件中,而不是将其放在不同的组件中。

Even though it clearly works, I wonder if it'll not impact rendering and performance once built.尽管它显然有效,但我想知道一旦构建它是否不会影响渲染和性能。 The makeStyles((theme) seems to work just fine but is not highlighted in VScode, probably because I used both of makeStyles , useTheme and ThemeProvider in the same file, as below: makeStyles((theme)似乎工作得很好,但在 VScode 中没有突出显示,可能是因为我在同一个文件中同时使用了makeStylesuseThemeThemeProvider ,如下所示:

import { makeStyles, useTheme } from '@material-ui/core/styles';
import { ThemeProvider } from '@material-ui/core/styles';
const useStyles = makeStyles((theme) => ({ ...
    (className example:) appBarShift: {
    width: `calc(100% - ${drawerWidth}px)`,
    marginLeft: drawerWidth,
    transition: theme.transitions.create(['margin', 'width'], {
      easing: theme.transitions.easing.easeOut,
      duration: theme.transitions.duration.enteringScreen,
    }),
}, ... etc

Was it a good idea to put it all together in _app.js, or do I need to refactor everything into a new component passing props to parent somehow?将所有内容放在 _app.js 中是个好主意,还是我需要将所有内容重构为一个新组件,以某种方式将 props 传递给父级?

Thanks for considering, best regards to all of you感谢您的考虑,向大家致以最诚挚的问候

I recommend you to put your theme inside a folder theme/theme.js and use createMuiTheme then in your app.js wrap your whole component with the theme provider.我建议您将主题放在文件夹 theme/theme.js 中并使用 createMuiTheme 然后在您的 app.js 中使用主题提供程序包装您的整个组件。 That would spread your theme across the app and let you use it where you want !这会将您的主题传播到整个应用程序,并让您在需要的地方使用它! Is the convention we got with material-ui.是我们使用 material-ui 得到的约定。

theme/theme.js:主题/theme.js:

import { createMuiTheme } from "@material-ui/core/styles"

export const themeObject = {
  palette: {
    primary: { main: "#333333" },
    secondary: { main: "#FD8C7C" },
  },
  backgroundColor: {
    primary: { main: "#FEF8F8" },
    secondary: { main: "#FFFFFF" },
  },
  breakpoints: {
    values: {
      xs: 0,
      sm: 600,
      md: 960,
      lg: 1280,
      xl: 1920,
    },
  },
  overrides: {
    MuiDivider: {
      root: {
        backgroundColor: "#FFFFFF",
      },
    },
  },
}

export default createMuiTheme(themeObject)

pages/app,js:页面/应用程序,js:


import React from "react"
import { ThemeProvider } from "@material-ui/styles"
import Box from "@material-ui/core/Box"
import defaultTheme from "../theme/theme"

const App = () => {
  return (
    <Box>
      <ThemeProvider theme={defaultTheme}>
       ...
      </ThemeProvider>
    </Box>
  )
}

export default App

component/SideBar.js:组件/SideBar.js:

import React from "react"
import { makeStyles } from "@material-ui/core/styles"

const useStyles = makeStyles((theme) => ({
  drawer: {
    width: theme.sidebar.width,
    color: theme.palette.primary
    flexShrink: 0,
    whiteSpace: "nowrap",
    "& .MuiDrawer-paper": {
      top: "unset",
    },
  },
  drawerClose: {
    transition: theme.transitions.create("width", {
      easing: theme.transitions.easing.sharp,
      duration: theme.transitions.duration.leavingScreen,
    }),
    overflowX: "hidden",
    width: theme.spacing(7) + 1,
    [theme.breakpoints.up("sm")]: {
      width: theme.spacing(7) + 1,
    },
  },
  expandButton: {
    [theme.breakpoints.up("sm")]: {
      marginLeft: theme.spacing(-2.5),
      marginRight: theme.spacing(2.5),
    },
  },
}))

export default function Sidebar(props) {
const classes = UseStyles()
  return (...)
}


In the SideBar you can see that i have use the theme.zindex.drawer who come from directly theme.js.在 SideBar 中,您可以看到我使用了直接来自 theme.js 的 theme.zindex.drawer。 I also recommend you to go check this MUI THEMING that would let you dive into the theming more deeply !我还推荐你到 go 检查这个MUI THEMING ,它会让你更深入地研究主题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅在“_app.js”中使用 MUI 5 ThemeProvider 是最佳实践,还是可以在每个组件上使用? - Is It Best Practice To Use MUI 5 ThemeProvider Only in "_app.js" or Is It Ok To Use On Every Component? Nodejs 如何添加另一个主 app.js 并使用它们来使我的代码干净? - Nodejs how can I add another main app.js and use both of them to make my code clean? 我可以在我的app.js文件中使用车把模板吗 - Can I use a handlebars template in my app.js file 我可以在 _app.js 和页面中使用 getInitialProps 吗? - Can I use getInitialProps in _app.js and in pages? 我们如何在另一个组件中创建一个抽屉导航并在 app.js 中调用它 - How can we create a drawer navigation in another component and just call it in the app.js 我们可以在一个主要的 App.js 文件中包含一个或多个自关闭组件吗 - Can we include one or more self closing components in one main App.js file 无法在 _app.js 中使用 getStaticProps - Unable to use getStaticProps in _app.js 我们如何将用户 ID 传递到 app.js 中的路由? - how do we pass the user id into a route in app.js? AngularJS:创建可在我的app.js文件中使用的类以使用超链接图片 - AngularJS: Creating a class that I can use in my app.js file to use hyperlink pictures React / MUI:如何在主 App.js 中使用钩子按钮调用其他 class - React / MUI : How to call other class with hook to button in main App.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM