我有问题,我使用这段代码来响应布局。在我的计算机中,当我使用编程工具观察我的代码在电话大小时,它看起来很完美,但是当我部署它时,这一点都不起作用。我不明白为什么,因为我在另一个子页面中有相同的代码,而且它在那里工作得很好。
import MediaQuery, { useMediaQuery } from "react-responsive";
const Phone = useMediaQuery({
query: "(max-width: 641px)"
});
<Card style={{ width: Phone ? '100%' : "32%", float: "left", backgroundColor: "#003263", height: "380px"}} >
发布于 2022-09-10 21:33:04
你好,我有同样的问题,我寻找了几个解决方案,但我最终使用的材料UI。
import React from 'react'
import useMediaQuery from '@mui/material/useMediaQuery'
const ProductCard = ({ name, category, description, tech, url, image, color }) => {
const isMobile = useMediaQuery('(max-width: 786px)')
return (
<div style={{ margin: isMobile ? '0px' : '18px' }}>
<p>{name}</p>
<p>{category}...</p>
</div>
)
}
export default ProductCard
它工作得非常好,在第一个渲染中,它正确地检测到变量并使css发生变化,但是您必须安装MUI。
https://stackoverflow.com/questions/73185898
复制