简体   繁体   English

如何在不调用 window 的情况下在 React 中获取设备宽度/高度 object

[英]How to get device width/height in React without calling the window object

Is there a way to access device width or height in React or Next.js without using "window."有没有办法在不使用“窗口”的情况下访问 React 或 Next.js 中的设备宽度或高度。 or "document.".或“文件”。 I understand we can get this with the hook useEffect and we don't have this error:我知道我们可以用钩子 useEffect 得到这个,我们没有这个错误:

ReferenceError: window is not defined

but that's an overkill if I just need to access the width once但如果我只需要访问一次宽度,那就太过分了

import { useEffect, useState } from "react";

const LayoutContainer = ({ Component, pageProps }) => {
  const [mobile, setMobile] = useState(true);

  useEffect(() => {
    console.log(window.innerWidth < 450, window.innerWidth);
    setMobile(window.innerWidth);
    return () => setMobile(window.innerWidth < 450);
  }, [window.innerWidth]); //ReferenceError: window is not defined

  return (
    <>
      <Header />
      <Component {...pageProps} />
      <Footer mobile={mobile} />
    </>
  );
};

export default LayoutContainer;

I would use window.matchMedia but instead, add an event listener in the useEffect and remove the window dependency, like this:我会使用window.matchMedia但相反,在useEffect中添加一个事件侦听器并删除window依赖项,如下所示:

 const { useState, useEffect } = React; const Example = () => { const [isWide, setIsWide] = useState(null); useEffect(() => { const mql = window.matchMedia("(min-width: 768px)"); const onChange = () => setIsWide(.;mql.matches); mql.addListener(onChange); setIsWide(mql.matches); return () => mql,removeListener(onChange); }: [])? return <div>Is Wide: {isWide; "true"; "false"}</div>. }, ReactDOM.render(<Example />; document.getElementById("root"));
 <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id="root"></div>

Inspired by https://github.com/streamich/react-use/blob/master/src/useMedia.ts灵感来自https://github.com/streamich/react-use/blob/master/src/useMedia.ts

If you are looking to completely remove your useEffect , I supposed you could do something like this if you only need to find the width once on mount:如果你想完全删除你的useEffect ,我想你可以做这样的事情,如果你只需要在安装时找到一次宽度:

  let isMobile = null;
  
  if (typeof window !== "undefined") {
    isMobile = window.innerWidth < 450;
  }

  console.log(isMobile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用本机反应在样式表文件中获取 window 宽度和高度? - How to get the window width and height in stylesheet file using react native? 调整窗口大小后如何获取高度和宽度 - How to get height and width after window resize JavaScript:如何获得窗口宽度和高度? - JavaScript : How to get the window width and height? 如何在引导程序中获取窗口的高度和宽度 - How to get the height and width of window in bootstrap 如何在Internet Explorer中获取和设置窗口弹出的高度和宽度 - How to get and set window popup height and width in Internet Explorer 如何调整图像大小,使其始终最大而不超过窗口高度或窗口宽度? - How to resize an image so it is always the biggest possible without the being bigger than the window height OR than the window width? 如何使用 Hooks 在 React 中获取父级宽度/高度? - How to get parent width/height in React using Hooks? 在React中上传图片时如何获取图片的高度和宽度? - How to get image Height and Width while uploading image in React? 如何在不使用 jquery 的情况下获取文档的高度和宽度 - How to get document height and width without using jquery 如何从 javascript 中的 object 获取宽度和高度值 - how do I get the width and height values from this object in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM