reactjs - 带显示的响应式处理:无
问题描述
我试图了解将 css 属性display: none
与媒体查询一起使用是否是一种在移动设备和桌面设备之间显示不同布局的安全方法。我正在为我的前端使用react
and material-ui
,并且随着我的布局变得更加复杂,我发现将display
属性设置为none
是一种“简单”的方法来显示各种屏幕尺寸的特定布局。
关于 SSR,我认为这比使用useMediaQuery挂钩更安全。我知道我可能会放弃性能,因为无论屏幕大小如何,渲染时都会调用移动和桌面组件。
除了性能(和使用基于类的组件)之外,我是否应该考虑其他任何东西作为这种方法的潜在缺陷?
import React from 'react';
import { withStyles } from '@material-ui/core';
import Mobile from './components/Mobile';
import Desktop from './components/Desktop';
const styles = theme => ({
hideOnMobile: {
// max-width media query between 0 and md
[theme.breakpoints.down('sm')]: {
display: 'none'
}
},
hideOnDesktop: {
// min-width media query from md and up
[theme.breakpoints.up('md')]: {
display: 'none'
}
}
});
class ResponsiveComponent extends React.Component {
render() {
const { classes } = this.props;
return (
<div>
<div className={classes.hideOnMobile}>
<Desktop />
</div>
<div className={classes.hideOnDesktop}>
<Mobile />
</div>
</div>
);
}
}
export default withStyles(styles)(ResponsiveComponent);
解决方案
推荐阅读
- javascript - 在 HTML/CSS 中调整按钮大小无法按预期工作
- java - 计算给定数的上界的程序
- php - how to get all values if ID tags from SVG sprite into php array
- python-3.x - Sort cards according to its suite and values using python
- python - 使用 azureSDK 在 python 中翻译扬声器输出中的音频
- java - Javascanner.nextLine() 没有读取 259094 个字符的长行
- javascript - 尝试使用驼峰式字符串,为什么我的字母没有更新为大写?
- python-3.x - 使用正则表达式调用的字符串中缺少所需的字符
- python-3.x - 无法使用 Docker 的 GPU。在 tensorflow GPU docker 镜像之上构建自定义 docker 容器镜像
- android - RecyclerView Adapter 上 TextWatchers 方法的无限循环