首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MUI5在导航栏上使用断点

使用MUI5在导航栏上使用断点
EN

Stack Overflow用户
提问于 2021-11-27 07:10:51
回答 1查看 329关注 0票数 2

我试图让我的导航响应使用断点,我已经阅读了文档,并试图实现我的代码如下图所示。

`

代码语言:javascript
复制
import * as React from 'react';
import { styled } from '@mui/material/styles';
import Typography from '@mui/material/Typography';
import { green } from '@mui/material/colors';
import { AppBar } from '@mui/material';

const styles = (theme) => ({
    root: {
        padding: theme.spacing(1),
        [theme.breakpoints.down('md')]: {
            backgroundColor: theme.palette.secondary.main,
        },
        [theme.breakpoints.up('md')]: {
            backgroundColor: theme.palette.primary.main,
        },
        [theme.breakpoints.up('lg')]: {
            backgroundColor: green[500],
        },
    },
});

export default function MediaQuery() {
    return (
        <AppBar sx={styles}>
            <Typography>red</Typography>
            <Typography>blue</Typography>
            <Typography>yellow</Typography>
        </AppBar>
    );
}
`
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-27 09:35:58

您需要从样式函数中移除根键:

代码语言:javascript
复制
import * as React from 'react';
import Typography from '@mui/material/Typography';
import { green } from '@mui/material/colors';
import { AppBar } from '@mui/material';

const styles = theme => ({
    padding: theme.spacing(1),
    [theme.breakpoints.down('md')]: {
        backgroundColor: theme.palette.secondary.main,
    },
    [theme.breakpoints.up('md')]: {
        backgroundColor: theme.palette.primary.main,
    },
    [theme.breakpoints.up('lg')]: {
        backgroundColor: green[500],
    },
});

export default function MediaQuery() {
    return (
        <AppBar sx={styles}>
            <Typography>red</Typography>
            <Typography>blue</Typography>
            <Typography>yellow</Typography>
        </AppBar>
    );
}

TS版本将是:

代码语言:javascript
复制
import Typography from '@mui/material/Typography';
import { green } from '@mui/material/colors';
import { AppBar, useTheme } from '@mui/material';

const styles = theme => ({
    padding: theme.spacing(1),
    [theme.breakpoints.down('md')]: {
        backgroundColor: theme.palette.secondary.main,
    },
    [theme.breakpoints.up('md')]: {
        backgroundColor: theme.palette.primary.main,
    },
    [theme.breakpoints.up('lg')]: {
        backgroundColor: green[500],
    },
});

export default function MediaQuery() {
    const theme = useTheme();
    return (
        <AppBar sx={styles(theme)}>
            <Typography>red</Typography>
            <Typography>blue</Typography>
            <Typography>yellow</Typography>
        </AppBar>
    );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70132745

复制
相关文章

相似问题

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