简体   繁体   English

Next.js 仅在桌面上渲染视频:useEffect 无法对未安装的组件执行 React state 更新

[英]Next.js render video only on desktop: useEffect can't perform a React state update on an unmounted component

I know there are multiple similar questions here already, but I can't see which asynchronous fix I need to make in order to make this work.我知道这里已经有多个类似的问题,但我看不出我需要进行哪个异步修复才能完成这项工作。

I have a Next.js website where I want to render a video but only when we're on desktop.我有一个 Next.js 网站,我想在其中呈现视频,但仅当我们在桌面上时。 I don't want the video at all to be there on mobile to save the user from downloading it in the first place, and in order to do that I wrote my own React hook to keep track of the window size.我根本不希望视频出现在移动设备上以防止用户首先下载它,为了做到这一点,我编写了自己的 React 钩子来跟踪 window 的大小。

I can use the isDesktop in my code and it all works, and since I am still in development phase I added the window resize listener so I can easily test things by just resizing my window rather than reloading the page each time.我可以在我的代码中使用isDesktop ,一切正常,因为我仍处于开发阶段,所以我添加了 window 调整大小侦听器,这样我就可以通过调整 window 的大小来轻松测试,而不是每次都重新加载页面。

Everything works as expected, except for this dreaded message:一切都按预期工作,除了这条可怕的消息:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

which is driving me nuts.这让我发疯。 I have this code:我有这段代码:

import { useState, useEffect } from 'react';

export function useWindowSize() {
    const isBrowser = typeof window !== 'undefined';
    const [isDesktop, setIsDesktop] = useState<boolean>(false);

    function update() {
        setIsDesktop(window.innerWidth >= 768);
    }

    useEffect(() => {
        if (isBrowser) {
            update();
            window.addEventListener('resize', update, false);
            return () => {
                window.removeEventListener('resize', update, false);
            };
        }
    }, [isBrowser]);

    return { isDesktop };
}

and as soon as I comment out the update call in the useEffect , the message disappears.一旦我在useEffect中注释掉update调用,消息就会消失。 But then I no longer get the initial state (meaning, it will not render the video on my desktop page until i start resizing the window, and that's not what should happen).但是后来我不再获得初始的 state(意思是,在我开始调整 window 的大小之前,它不会在我的桌面页面上呈现视频,这不是应该发生的事情)。

The problem is that you are conditionally calling the useEffect cleanup function, it should always be static. Try putting the isBrowser condition inside the cleanup function问题是您有条件地调用 useEffect 清理 function,它应该始终为 static。尝试将isBrowser条件放入清理 function

useEffect(() => {
  if (isBrowser) {
    update();
    window.addEventListener('resize', update, false);
  }

  return () => {
    isBrowser && window.removeEventListener('resize', update, false);
  };
}, [isBrowser]);

暂无
暂无

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

相关问题 无法使用 useEffect 挂钩对未安装的组件执行 React state 更新 - Can't perform a React state update on an unmounted component with useEffect hook 无法使用 useEffect 对未安装的组件问题执行反应状态更新 - can't perform a react state update on an unmounted component issue with useEffect React:“无法在未安装的组件上执行 React 状态更新”没有 useEffect 函数 - React: "Can't perform a React state update on an unmounted component" without useEffect function 无法对未安装的组件执行 React state 更新。 使用效果 React-Native - Can't perform a React state update on an unmounted component. useEffect React-Native React useEffect 导致:无法对未安装的组件执行 React 状态更新 - React useEffect causing: Can't perform a React state update on an unmounted component 在useEffect清理函数执行之前获取“无法对卸载的组件执行React状态更新” - Getting 'Can't perform a React state update on an unmounted component' before useEffect cleanup function executes 警告:无法在没有 useEffect 的情况下对未安装的组件执行 React state 更新 - Warning: Can't perform a React state update on an unmounted component without useEffect UseEffect 挂钩错误,无法对未安装的组件执行 React state 更新 - UseEffect hook error, Can't perform a React state update on an unmounted component 警告:无法对未安装的组件执行 React state 更新。 使用效果清理 function - Warning: Can't perform a React state update on an unmounted component. useEffect cleanup function React - 无法在未安装的组件上执行 React state 更新 - React - Can't perform a React state update on an unmounted component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM