小编Ale*_*ner的帖子

NextJs 13.2 中的 React 上下文

我尝试在 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);
Run Code Online (Sandbox Code Playgroud)

然后我包装我的布局 -

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 …
Run Code Online (Sandbox Code Playgroud)

next.js react-context

5
推荐指数
0
解决办法
444
查看次数

标签 统计

next.js ×1

react-context ×1