对于材质UI V.5,Make Styles的替代方案是什么 [英] What is the alternative of makeStyles for Material UI v.5

查看:12
本文介绍了对于材质UI V.5,Make Styles的替代方案是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Material UI版本5。最初为了使用我的自定义主题样式,我会使用makestyle,但似乎在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>
    );
}

我读过有关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

我已经将我的主题导入到我的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;

我确实查看了你们几个发给我的一些文档,并且我正在查看故障排除部分,其中显示";[type]property";Palette";,";space";不存在于类型‘DefaultTheme’";上,因为makeStyles以不同的方式导出,并且它不知道主题。似乎有一个代码段可以放入TypeScript项目中(我没有运行该项目,我使用的是javascript),并且有一个部分用于将ts文件添加到我的javascript中,并放入它推荐的代码段(我试过了),但是我是否遗漏了什么东西,因为它什么都没有做,并且我不确定是否需要在App.js文件中放入一些东西才能让它读取?

推荐答案

您仍然可以将makeStyles实用程序用作您正在使用的工具,但是在Material v5中,如果您喜欢这样做,则需要再安装一个软件包@mui/styles

import { makeStyles } from '@mui/styles';

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

makeStyles JSS实用程序不再从@mui/Material/Style导出。您可以改用@MUI/Style/makeStyles。

此外,如果需要,您还需要将tabpurple添加到createTheme

const theme = createTheme({
  typography: {
    tab: {
      fontSize: 20,
    },
  },
  palette: {
    common: {
      purple: 'purple',
    },
  },
})

这篇关于对于材质UI V.5,Make Styles的替代方案是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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