Nestjs 13自定义布局未显示

huangapple go评论81阅读模式
英文:

Nestjs 13 custom layout didn't show up

问题

我使用 NextJs 13 构建我的项目。
<br>
我想将 CustomLayout 作为整个网站的布局。
<br>
不再有错误,但 CustomLayout 没有显示出来。
<br>
但它只显示 `<Home/>` 页面。
<br>
如何让用户在 CustomLayout 中点击菜单,它只会更改 `{page}`
英文:

I use NextJs 13 to build my project.
<br>
I want CustomLayout as the whole website's Layout.
<br>
There's no error anymore but CustomLayout doesn't show up.
<br>
But it only show &lt;Home/&gt; page.
<br>
How can let user click menu in CustomLayout, and it will only change {page}?

app/CustomLayout.tsx

&#39;use client&#39;
import { useState, useRef, useEffect } from &#39;react&#39;
import &#39;./globals.css&#39;
import &#39;./index.scss&#39;
import Menu from &#39;@/app/components/Menu&#39;
import PrelodingPage from &#39;@/app/components/Preloading&#39;
import Cursor from &#39;@/app/components/cursor/CustomCursor&#39;
import CursorManager from &#39;@/app/components/cursor/CursorManager&#39;

export default function Layout(props: any) {
  const [preloading, setPreloading] = useState&lt;boolean&gt;(true)
  const [menuToggle, setMenuToggle] = useState&lt;boolean&gt;(false)
  const intervalRef: { current: NodeJS.Timeout | null } = useRef(null)
  const clear = () =&gt; {
    clearInterval(intervalRef.current as NodeJS.Timeout)
  }

  useEffect(() =&gt; {
    const id = setInterval(() =&gt; {
      setPreloading(false)
    }, 3000)
    intervalRef.current = id
  }, [])

  useEffect(() =&gt; {
    if (!preloading) {
      clear()
    }
  }, [preloading])

  const classes = menuToggle ? &#39;menu-button active&#39; : &#39;menu-button&#39;

  return (
    &lt;CursorManager&gt;
      {preloading ? (
        &lt;PrelodingPage /&gt;
      ) : (
        &lt;div className=&#39;main&#39;&gt;
          &lt;&gt;
            &lt;button
              className={classes}
              onClick={() =&gt; setMenuToggle(!menuToggle)}
            &gt;
              &lt;span className=&#39;bar&#39;&gt;&lt;/span&gt;
            &lt;/button&gt;
            &lt;Cursor /&gt;
            &lt;Menu menuToggle={menuToggle} setMenuToggle={setMenuToggle} /&gt;
          &lt;/&gt;
        &lt;/div&gt;
      )}
    &lt;/CursorManager&gt;
  )
}

app/page.tsx

import type { ReactElement } from &#39;react&#39;
import Layout from &#39;./layout&#39;
import CustomLayout from &#39;./CustomLayout&#39;
import Home from &#39;./home/page&#39;
import type { NextPageWithLayout } from &#39;./_app&#39;

const Page: NextPageWithLayout = () =&gt; {
  return &lt;Home /&gt;
}

Page.getLayout = function getLayout(page: ReactElement) {
  return (
    &lt;Layout&gt;
      &lt;CustomLayout&gt;{page}&lt;/CustomLayout&gt;
    &lt;/Layout&gt;
  )
}

export default Page

答案1

得分: 1

以下是代码部分的翻译:

export default function RootLayout({ children }: {
  children: React.ReactNode;
}) {
  return (
    &lt;html lang=&quot;en&quot;&gt;
      &lt;body&gt;{children}&lt;/body&gt;
    &lt;/html&gt;
  );
}
export default function Layout({ children }: {
  children: React.ReactNode;
}) {
  const [preloading, setPreloading] = useState&lt;boolean&gt;(true)
  const [menuToggle, setMenuToggle] = useState&lt;boolean&gt;(false)
  const intervalRef: { current: NodeJS.Timeout | null } = useRef(null)
  const clear = () =&gt; {
    clearInterval(intervalRef.current as NodeJS.Timeout)
  }

  useEffect(() =&gt; {
    const id = setInterval(() =&gt; {
      setPreloading(false)
    }, 3000)
    intervalRef.current = id
  }, [])

  useEffect(() =&gt; {
    if (!preloading) {
      clear()
    }
  }, [preloading])

  const classes = menuToggle ? &#39;menu-button active&#39; : &#39;menu-button&#39;

  return (
    &lt;CursorManager&gt;
      {preloading ? (
        &lt;PrelodingPage /&gt;
      ) : (
        &lt;div className=&#39;main&#39;&gt;
          &lt;&gt;
            &lt;button
              className={classes}
              onClick={() =&gt; setMenuToggle(!menuToggle)}
            &gt;
              &lt;span className=&#39;bar&#39;&gt;&lt;/span&gt;
            &lt;/button&gt;
            &lt;Cursor /&gt;
            &lt;Menu menuToggle={menuToggle} setMenuToggle={setMenuToggle} /&gt;
          &lt;/&gt;
        &lt;/div&gt;
      ):
    {children}
    }
    &lt;/CursorManager&gt;
  )
}

如果你需要更多翻译或有其他问题,请随时提问。

英文:

Next js 13 layout

The root layout is defined at the top level of the app directory and applies to all routes. This layout enables you to modify the initial HTML returned from the server.

>you can't add custom names like CustomLayout.tsx. The layout should be only names as layout.tsx/jsx

>Layouts should always pass down the children prop because of their nested structure

export default function RootLayout({ children }: {
children: React.ReactNode;
}) {
return (
&lt;html lang=&quot;en&quot;&gt;
&lt;body&gt;{children}&lt;/body&gt;
&lt;/html&gt;
);
}

Next js 13 layout documentation

Your Code Should Be Like This

app/layout.tsx

>! The root layout is defined at the top level of the app directory and applies to all routes. This layout enables you to modify the initial HTML returned from the server.

&#39;use client&#39;
import { useState, useRef, useEffect } from &#39;react&#39;
import &#39;./globals.css&#39;
import &#39;./index.scss&#39;
import Menu from &#39;@/app/components/Menu&#39;
import PrelodingPage from &#39;@/app/components/Preloading&#39;
import Cursor from &#39;@/app/components/cursor/CustomCursor&#39;
import CursorManager from &#39;@/app/components/cursor/CursorManager&#39;
export default function Layout({ children }: {
children: React.ReactNode;
}) {
const [preloading, setPreloading] = useState&lt;boolean&gt;(true)
const [menuToggle, setMenuToggle] = useState&lt;boolean&gt;(false)
const intervalRef: { current: NodeJS.Timeout | null } = useRef(null)
const clear = () =&gt; {
clearInterval(intervalRef.current as NodeJS.Timeout)
}
useEffect(() =&gt; {
const id = setInterval(() =&gt; {
setPreloading(false)
}, 3000)
intervalRef.current = id
}, [])
useEffect(() =&gt; {
if (!preloading) {
clear()
}
}, [preloading])
const classes = menuToggle ? &#39;menu-button active&#39; : &#39;menu-button&#39;
return (
&lt;CursorManager&gt;
{preloading ? (
&lt;PrelodingPage /&gt;
) : (
&lt;div className=&#39;main&#39;&gt;
&lt;&gt;
&lt;button
className={classes}
onClick={() =&gt; setMenuToggle(!menuToggle)}
&gt;
&lt;span className=&#39;bar&#39;&gt;&lt;/span&gt;
&lt;/button&gt;
&lt;Cursor /&gt;
&lt;Menu menuToggle={menuToggle} setMenuToggle={setMenuToggle} /&gt;
&lt;/&gt;
&lt;/div&gt;
):
{children}
}
&lt;/CursorManager&gt;
)
}

huangapple
  • 本文由 发表于 2023年3月12日 14:23:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定