Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当屏幕宽度改变时,如何在屏幕上获取React App change内容_Javascript_Reactjs_Web_Browser - Fatal编程技术网

Javascript 当屏幕宽度改变时,如何在屏幕上获取React App change内容

Javascript 当屏幕宽度改变时,如何在屏幕上获取React App change内容,javascript,reactjs,web,browser,Javascript,Reactjs,Web,Browser,我的React项目中有一些组件,这些组件将仅用于移动设备 我是这样设置的- const LandingPage = (props) => { const [isMobile, setIsMobile] = useState(false); useEffect(() => { window.screen.width <= 760 ? setIsMobile(true) : setIsMobile(false); }, [window.screen.width]

我的React项目中有一些组件,这些组件将仅用于移动设备

我是这样设置的-

const LandingPage = (props) => {
  const [isMobile, setIsMobile] = useState(false);
  useEffect(() => {
    window.screen.width <= 760 ? setIsMobile(true) : setIsMobile(false);
  }, [window.screen.width]);

   **...Components render here**
}
const LandingPage=(道具)=>{
const[isMobile,setIsMobile]=useState(false);
useffect(()=>{

window.screen.width您可以使用window.onresize检测窗口大小的变化,然后将该值与window.innerWidth进行比较

const LandingPage = (props) => {
    
    const [isMobile, setIsMobile] = useState(false);

    useEffect(() => {
      window.screen.width <= 760 ? setIsMobile(true) : setIsMobile(false);
    }, [window.screen.width]);

    function detectWindowSize() {
        window.innerWidth <= 760 ? setIsMobile(true) : setIsMobile(false);        
    }
    
    window.onresize = detectWindowSize;

    console.log(isMobile)

     return(
         <div>

         </div>
     )
}
const LandingPage=(道具)=>{
const[isMobile,setIsMobile]=useState(false);
useffect(()=>{

window.screen.width您可以使用window.onresize检测窗口大小的变化,然后将该值与window.innerWidth进行比较

const LandingPage = (props) => {
    
    const [isMobile, setIsMobile] = useState(false);

    useEffect(() => {
      window.screen.width <= 760 ? setIsMobile(true) : setIsMobile(false);
    }, [window.screen.width]);

    function detectWindowSize() {
        window.innerWidth <= 760 ? setIsMobile(true) : setIsMobile(false);        
    }
    
    window.onresize = detectWindowSize;

    console.log(isMobile)

     return(
         <div>

         </div>
     )
}
const LandingPage=(道具)=>{
const[isMobile,setIsMobile]=useState(false);
useffect(()=>{

window.screen.width您希望通过检查用户是从移动设备还是桌面设备进行访问,为用户在页面加载时做出决定


当屏幕分辨率发生变化时,在桌面上加载移动组件不是最好的方式,因为这是一个非常有限的用例。没有多少桌面用户在使用你的Web应用时会将窗口拉得那么小。但你完全可以在向移动用户提供移动视图和组件时,使用CSS作为备用方法来解释这一点仅限。

您希望通过检查用户是从移动设备还是桌面设备进行访问来为用户在页面加载时做出决定


当屏幕分辨率发生变化时,在桌面上加载移动组件不是最好的方式,因为这是一个非常有限的用例。没有多少桌面用户在使用你的Web应用时会将窗口拉得那么小。但你完全可以在向移动用户提供移动视图和组件时,使用CSS作为备用方法来解释这一点只有。

我有一个类似的问题,并在这里用答案解决了:我有一个类似的问题,并在这里用答案解决了:我尝试了这个,但我的应用程序在添加了这个代码后没有加载。在
useffect()中添加它之后,它只是一个空白的白色屏幕显示
这与以前的行为相同,您需要将其置于useEffect之外。我编辑了代码谢谢,我正在执行此操作
window.onresize=detectWindowSize()
。这是导致问题的原因。我尝试了此操作,添加此代码后,我的应用程序无法加载。在
useEffect()中添加此代码后,只会显示一个空白的白色屏幕
这与以前的行为相同,您需要将其置于useEffect之外。我编辑了代码谢谢,我正在这样做
window.onresize=detectWindowSize()
。这就是问题的原因