NextJS 13 Error / Error: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it

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

NextJS 13 Error / Error: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it

问题

错误:createContext仅在客户端组件中有效。在文件顶部添加"use client"指令以使用它。

我可以告诉你为什么会发生这个错误吗?

英文:

**Error: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it.
**
Can you tell me why the error occurs?

  1. // layout.tsx
  2. import Layout from "./components/common/Layout";
  3. export default function RootLayout({
  4. children,
  5. }: {
  6. children: React.ReactNode;
  7. }) {
  8. return (
  9. <html lang="en">
  10. <body>
  11. <Layout>{children}</Layout>
  12. </body>
  13. </html>
  14. );
  15. }
  1. // Layout.tsx
  2. "use client";
  3. import React, { useState, ReactNode } from "react";
  4. import Nav from "@app/components/common/Nav";
  5. import { Global, css } from "@emotion/react";
  6. import { PageContextProvider } from "store/pageContext";
  7. import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
  8. export default function Layout({ children }: { children: ReactNode }) {
  9. const [queryClient] = useState(
  10. () =>
  11. new QueryClient({
  12. defaultOptions: {
  13. queries: {
  14. refetchOnWindowFocus: false,
  15. staleTime: 5000,
  16. },
  17. },
  18. })
  19. );
  20. return (
  21. <div>
  22. <QueryClientProvider client={queryClient}>
  23. <Global styles={reset} />
  24. <PageContextProvider>
  25. <Nav />
  26. <div>{children}</div>
  27. </PageContextProvider>
  28. </QueryClientProvider>
  29. </div>
  30. );
  31. }
  1. // pageContext.tsx
  2. "use client";
  3. import React, { createContext, useState, useRef } from "react";
  4. interface PageContextType {
  5. currentPage: number;
  6. setCurrentPage: React.Dispatch<React.SetStateAction<number>>;
  7. sectionsRef: React.MutableRefObject<(HTMLElement | null)[]>;
  8. }
  9. export const PageContext = createContext<PageContextType>({
  10. currentPage: 0,
  11. setCurrentPage: () => {},
  12. sectionsRef: {} as React.MutableRefObject<(HTMLElement | null)[]>,
  13. });
  14. export const PageContextProvider = ({
  15. children,
  16. }: {
  17. children: React.ReactNode;
  18. }) => {
  19. const [currentPage, setCurrentPage] = useState<number>(0);
  20. const sectionsRef = useRef<(HTMLDivElement | null)[]>([]);
  21. return (
  22. <PageContext.Provider value={{ currentPage, setCurrentPage, sectionsRef }}>
  23. {children}
  24. </PageContext.Provider>
  25. );
  26. };

I want to use the layout.tsx file as a server component and add metadata, but if I don't use "use client" in the layout file, an error occurs. Do you know anyone?

答案1

得分: 1

你可以将 QueryClient 的初始化移动到一个单独的组件中(该组件将具有 useClient),然后在您的 Layout 组件中导入它,并在那里移除 useClient。这样,您的布局组件将成为一个服务器组件。

英文:

You can move the init of QueryClient into a separate component (which will have use client), import it in your Layout component and remove the use client there.
This way your layout component will be a server component.

huangapple
  • 本文由 发表于 2023年7月3日 16:30:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603079.html
匿名

发表评论

匿名网友

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

确定