Material UI v.5 的 makeStyles 的替代方案是什么

Nat*_*uki 37 javascript reactjs material-ui

我刚刚开始使用 Material UI 版本 5。最初为了使用我的自定义主题样式,我会使用 makestyles,但似乎在 v.5 中不起作用。我的主题位于其自己的组件上,为了导入这些主题,我使用{createTheme}而不是旧的{createMuiTheme}. 我像往常一样将主题导入到父组件中,并将其设置为<ThemeProvider theme{theme}></ThemeProvider>.

现在,在我的另一个组件上,我再次尝试使用 useStyles,但它不起作用,因为它没有在版本 5 中使用。我很难弄清楚如何转换它,以便它可以在版本 5。以下是我正在编写的一些未完成的代码:

const useStyles = makeStyles((theme) => ({
    logo: {
        height: "8em",
        marginLeft: "0.2em",
    },
    tabContainer: {
        marginLeft: "auto",
    },
    tab: {
        ...theme.typography.tab,
        minWidth: 10,
        marginRight: "50px",
        opacity: 1,
        "&hover": {
            color: theme.palette.common.purple,
            textDecoration:"none",
        },
    },
}));

export default function Navigation(props) {
    const classes = useStyles();

    const [value, setValue] = useState(0);

    const handleChange = (e, value) => {
        setValue(value);
    };
    const refreshPage = () => {
        window.location.reload();
    };
    
    useEffect(() => {
        switch (window.location.pathname) {
            case "/":
                if (value !== 0) {
                    setValue(0);
                }
                break;
                default:
                break;
        }
    }, [value]);


    return (
      <React.Fragment>
        <ElevationScroll>
          <AppBar
            position="relative"
            style={{
              borderBottom: "2px solid black",
            }}
          >
            <Toolbar disableGutters>
                <img src={logo} alt="nasa logo" className={classes.logo}/>
                <Typography variant="h1" style={{ textAlign: "center" }}>
                  Nasa<br></br>Photos
                </Typography>
                <Tabs
                  value={value}
                  onChange={handleChange}
                  className={classes.tabContainer}
                  indicatorColor="primary"
                >
                  <Tab
                    className={classes.tab}
                    component={Link}
                    onClick={refreshPage}
                    to="/"
                    label="Home"
                  />
                </Tabs>
            </Toolbar>
          </AppBar>
        </ElevationScroll>
      </React.Fragment>
    );
}
Run Code Online (Sandbox Code Playgroud)

我已经阅读过有关 xs 属性的内容,并且还通过 Material UI 的文档听说过 styled(),但我很难将其应用到我编写的代码中,并且希望将其推向正确的方向。

因此,为了编辑我之前的内容,我还将添加我的 Theme.js 文件。我认为这已经正确完成,但它又没有读取我的选项卡或调色板。

import {createTheme} from "@mui/material/styles";

const pink = "#FFC0CB";
const lightblue = "#ADD8E6";
const purple = "#800080";
const black = "#000000";

const theme = createTheme({
    palette: {
        common: {
            pink: pink,
            lightblue: lightblue,
            purple: purple,
            black: black
        },
        primary: {
            main: pink,
            mainGradient: "linear-gradient(to left, purple, pink)",
        },
        secondary: {
            main: lightblue,
            mainGradient: "linear-gradient(to right, lightblue, pink)"
        },
    },
    typography: {
        tab: {
            fontFamily:"Orbitron",
            textTransform: "none",
            fontSize: "2.5rem",
            color: black,
        },
        h1: {
            fontFamily: "Orbitron",
            fontSize: "2.5em"
        },
        h2: {
            fontFamily: "Orbitron",
            fontSize: "2.5em"
        },
        subtitle1: {
            fontFamily: "Orbitron"
        },
        subtitle2: {
            fontFamily: "Orbitron",
            fontSize: "1.5rem"
        },
        buttons: {
            fontFamily: "Orbitron",
            textTransform: "none"
        },
    },
});

export default theme
Run Code Online (Sandbox Code Playgroud)

我已将主题导入到我的 App.js 文件中,这是我的顶级文件,我将在此处包含该主题,以防万一出现问题:

import React,{useState} from "react";
import PicOfDay from "./Components/PictureOfDay";
import Navigation from "./Components/Navigation";
import {
  Typography,
} from "@mui/material";
import {ThemeProvider} from '@mui/material/styles'
import theme from "../src/ui/Theme";
import {BrowserRouter as Router} from "react-router-dom";

function App(props) {
  const [date, setDate] = useState(new Date());
  return (
    <ThemeProvider theme={theme}>
      <Router>
        <Navigation date={date} setDate={setDate} />
        <Typography
          variant="h1"
          style={{fontSize: "5rem", textAlign: "center", marginTop:"2rem"}}
          >
            Astronomy Picture of the Day
        </Typography>  
        {/* <PicOfDay date={date} /> */}
      </Router>
    </ThemeProvider>
  );
}

export default App;
Run Code Online (Sandbox Code Playgroud)

我确实看了你们几个发给我的一些文档,我正在查看故障排除部分,其中写着“[Types] Property“palette”,“spacing”在类型“DefaultTheme”上不存在”,因为 makeStyles 的导出方式不同它不知道主题。似乎有一个片段可以放入打字稿项目(我没有运行,我正在使用 javascript),并且有一个部分可以将 ts 文件添加到我的 javascript 并放置它推荐的片段,我尝试过,但是我我错过了一些东西,因为它没有做任何事情,我不确定是否需要在我的 App.js 文件中放入一些东西才能让它读取它?

Fre*_*yen 13

您仍然可以makeStyles像您正在使用的那样使用 utils,但在材料 v5 中,如果您喜欢这样做,您需要再安装一个软件包@mui/styles,然后

import { makeStyles } from '@mui/styles';
Run Code Online (Sandbox Code Playgroud)

https://mui.com/guides/migration-v4/#mui-material-styles

makeStyles JSS 实用程序不再从 @mui/material/styles 导出。您可以使用@mui/styles/makeStyles 代替。

另外,如果需要的话,还需要添加tabpurpletocreateTheme

const theme = createTheme({
  typography: {
    tab: {
      fontSize: 20,
    },
  },
  palette: {
    common: {
      purple: 'purple',
    },
  },
})
Run Code Online (Sandbox Code Playgroud)

  • @mui/styles 是 MUI 的遗留样式解决方案。它在 v5 中已弃用。 (4认同)
  • 好的,在进行这种迁移时,一件重要的事情*您需要在组件树顶部有带有“injectFirst”选项的“StyledEngineProvider”* https://mui.com/guides/migration-v4/#style-library (3认同)

Rez*_*eza 12

基于文档

@mui/styles 是 MUI 的遗留样式解决方案。它在 v5 中已弃用。它依赖于 JSS 作为样式解决方案,@mui/material 中不再使用 JSS。

您可以使用-sx-propstyled