NextJS + Tauri 中的窗口未定义

分享于2023年02月08日 electron next.js reactjs rust tauri 问答
【问题标题】:Window is undefined in NextJS + TauriNextJS + Tauri 中的窗口未定义
【发布时间】:2022-12-24 02:03:57
【问题描述】:

我想做什么:

我使用来自 Tauri 的 appWindow 访问 appWindow.minimize() appWindow.toggleMaximize() appWindow.close() 以创建 custom title bar

代码是什么样的:

import { appWindow } from "@tauri-apps/api/window";

const CustomTitleBar = () => {
  const hasLoaded = hasLoadedCSR(); // custom hook for checking if component has mounted using useEffect

  if (typeof window === "undefined") return <>; // 1st attempt to disable s-s-r for this component
  if (!hasLoaded) return <>; // 2nd attempt to disable s-s-r for this component
  return (
    <>
      
); }; export default CustomTitleBar;

我基本上做了 2 次尝试来解决这个问题,因为我绝对认为这是由 F***Lars 在类似问题中提到的 s-s-r 引起的。


【解决方案1】:

为了解决这个问题,我基本上使用 dynamic 函数创建了另一个组件来强制 CustomTitleBar 的 CSR。

import dynamic from "next/dynamic";

const DynamicCustomTitleBar = dynamic(() => import("./CustomTitleBar"), {
  s-s-r: false,
});

export default DynamicCustomTitleBar;

【讨论】: