reactjs - 在 React 中响应式地隐藏元素
问题描述
我正在使用 Reactstrap ,并且想在设备移动时隐藏我的输入元素。我使用的 className 与我们在 bootstrap 中使用的相同,但是 className 根本不起作用。有什么方法可以像 reactstap 中的 bootstrap 那样做?
<input className="d-sm-none d-md-block" type="text"placeholder="Search"/>
我试过上面的代码来隐藏它,但它不起作用,有什么办法吗?
解决方案
在正在呈现输入的父组件中;
这是假设您有功能性反应组件。相同的逻辑可以应用于 Class 组件。
useEffect(() => {
setDeviceWidth(window.innerWidth);
const setDeviceWidth = width => {
if (width < 767) {
return setDevice("mobile");
} else if (width > 767 && width < 1280) {
return setDevice("tablet");
}
return setDevice("desktop");
};
}, []);
管理状态
const [device,setDevice]=useState("")
有条件地渲染输入
{device==="mobile"?null:<input/>}
推荐阅读
- java - 使用 JPA + spring boot 获取所有状态的计数
- javascript - 如何通过 JavaScript 获取伪类样式?
- swiftui - 将 ORSSerialPort 库与 Combine 和 SwiftUi 一起使用。尝试订阅 Key-Value Observing(KVO) 对象时出现问题
- php - 更新数据多个codeigniter
- javascript - 直接在 document.body 上调用 DOM 方法 appendChild 与首先使用 queryselector 有什么区别
- python - Django:NameError:未定义名称'X'
- r - 如何在 R 的列中查找和替换整数中的十进制数?
- javascript - 如何使用 javascript 将 .txt 文件的内容作为数组读取?
- python - 为什么我的 PyGame Sprite 在一个组中未绘制 - AttributeError: 'Group' object has no attribute 'blitme'
- opengl - 为什么 glBeginTransformFeedback 需要指定原始类型?