Next.js 上下文提供程序使用页面特定布局组件包装 App 组件,提供未定义的数据

hel*_*llo 10 javascript reactjs next.js react-hooks

我有一个身份验证上下文组件,我在其中包装我的主应用程序组件,但同时我也尝试根据此处的 Next.js 文档执行页面特定的布局组件: https: //nextjs.org/docs/basic -功能/布局#每页布局

我这样做是否正确,因为我似乎无法从上下文提供程序获取数据。

/context/AuthContext.js

const UserContext = createContext({});

export default function AuthContext({children}) {
  // .. code
  return (
    <UserContext.Provider value={{ user, setUser }}>
      {children}
    </UserContext.Provider>
  );
}

export const useUser = () => useContext(UserContext);
Run Code Online (Sandbox Code Playgroud)

/_app.js

function MyApp({ Component, pageProps }) {    

  const getLayout = Component.getLayout || ((page) => page);

  return getLayout(
    <div>
      <AuthContext>
        <Component {...pageProps} />
      </AuthContext>
    </div>
  );
}

export default MyApp;
Run Code Online (Sandbox Code Playgroud)

/components/Project/List.js

import { useUser } from "../../context/AuthContext";

const ProjectList = () => {
  const { user } = useUser();
  console.log("get user data", user);

  return (
    <>
      test
    </>
  );
};

export default ProjectList;
Run Code Online (Sandbox Code Playgroud)

我正在尝试控制台登录用户,但它给了我未定义的信息。我想这是因为它被包装为布局组件的方式?我可能做错了。但我在我AuthContext的用户中进行了控制台登录,并且那里的信息是正确的。

/pages/projects/index.js

const Projects = () => {
  // code goes here
  return (
    <div>
      code goes here
    </div> 
  )
}

export default Projects;

Projects.getLayout = function getLayout(page) {
  return <ProjectLayout>{page}</ProjectLayout>;
};
Run Code Online (Sandbox Code Playgroud)

当我删除Projects.getLayout代码块时,数据会回来,但是当我添加此代码时,数据就会消失。

/components/Project/Layout.js

const ProjectLayout = ({children}) => {
  return (
    <>
      <ProjectList />
      {children}
    </>
  }

export default ProjectLayout
Run Code Online (Sandbox Code Playgroud)

jul*_*ves 33

您当前的结构ProjectLayout没有被 包裹AuthContext,这意味着您将无法访问其上下文。

您可以修改 的_app结构并移动getLayout调用,以便上下文正确地包装它。

function MyApp({ Component, pageProps }) {    
    const getLayout = Component.getLayout || ((page) => page);

    return (
        <AuthContext>
            {getLayout(<Component {...pageProps} />)}
        </AuthContext>
    );
}
Run Code Online (Sandbox Code Playgroud)

  • 你明星@juliomalves,这已经困扰我几个月了 - 我没有意识到你可以在 getLayout 被包装的情况下这样做 (3认同)