【发布时间】:2021-02-13 08:25:09
【问题描述】:
我正在尝试在 React 中实现 here 的媒体查询概念。如下所示,我得到以下信息:
TypeError: isMobile.addListener is not a function
function App() {
// Saving current page to state
let location = useLocation();
useEffect(() => {
useStore.setState({ currentPage: location })
}, [location]);
// Check if viewing in landscape on mobile
const isMobile = window.matchMedia("only screen and (max-width: 830px)").matches;
const setOrientation = (e) => {
if (e.isMobile) {
if (window.innerHeight > window.innerWidth){
// is not landscape
} else{
// is landscape
}
}
}
useEffect(() => {
setOrientation(isMobile)
isMobile.addListener(setOrientation)
return () => isMobile.removeEventListener(setOrientation)
}, []);
return (
<div className="App"></div>
);
}
export default App;
我做错了什么?
【问题讨论】:
标签: javascript reactjs media-queries addeventlistener