NextJs 13.2 中的 React 上下文

问题描述 投票:0回答:0

我试图在 NextJs 13 中使用 React 上下文,我遇到了一个错误

这是我的语境-

'use client';
import { MutableRefObject, createContext, useRef, useContext } from "react";

interface ContextProviderProps{
    children: React.ReactNode;
  }

const GlobalContext = createContext({});

export const ContextProvider:React.FC<ContextProviderProps> = ({children}) => {
    const ref = useRef< MutableRefObject<HTMLElement>>(null);

 return (
    <GlobalContext.Provider value={ref}>
        {children}
    </GlobalContext.Provider>
 )
}

export const useGlobalContext = () => useContext(GlobalContext);

然后我包装我的布局 -

export default function RootLayout({children}: LayoutProps) {
 
  return (
    <html className={`h-screen snap-y snap-mandatory scroll-smooth overflow-y-auto hide-scrollbar ${inter.className}`} lang="ru">
      <body className='text-lg px-3 py-6 max-w-7xl mx-auto my-0 bg-[#000] text-[#f2d6d6]'>
        <ContextProvider>
          {children}
        </ContextProvider>
      </body>
    </html>
  )
}

我是 13 版本的新手,我也尝试在页面中使用“uce 客户端”但没有成功。 请帮忙!

next.js react-context
© www.soinside.com 2019 - 2024. All rights reserved.