小编Jak*_*ake的帖子

有没有更好的方法来根据反应应用程序的屏幕尺寸更改样式?

我在我的 React 应用程序上使用 Material UI,并且使用 mui 中的 useMediaQuery 和 useTheme。这是我现在拥有的代码。有没有更好的方法来优化更少的代码?两个代码之间只有一些样式变化。

const MainPage = () => {
    const theme = useTheme();
    const isMatch = useMediaQuery(theme.breakpoints.down('md'))
    return (
        <div className='mainPage'>
            {
                isMatch ? (
                    <>
                        <Box sx={{ display: "flex", justifyContent: "center", alignContent: "center", flexDirection: "column", padding: "60px 10px 10px 10px" }}>
                            <Box component="img" src={LandingImage} sx={{ width: "100%" }} />
                            <Box sx={{ paddingTop: 8 }}>
                                <Typography sx={{ fontSize: 26, fontWeight: "bold", fontFamily: "sans-serif", textAlign: "center", paddingBottom: 5 }}>About us</Typography>
                            </Box>
                        </Box>
                    </>
                ) …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs material-ui

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

javascript ×1

material-ui ×1

reactjs ×1