-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(react): support importing "next-auth/react" in Server Component environment #5718
fix(react): support importing "next-auth/react" in Server Component environment #5718
Conversation
Currently, importing "next-auth/react" in a Server Component module (or in a module imported by a Server Component module) will fail with `Uncaught TypeError: React.createContext is not a function`. This is because React Context is unavailable in Server Components. With this change, "next-auth/react" can be imported without error, and an informative error is thrown if there's an attempt to actually *use* React Context.
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
Thanks, we plan to design a better API for the Next.js app directory, but as for now, the recommendation is to add the |
That's basically what I'm doing... "use client";
// This module exists for the sole purpose of cloning SessionProvider & adding the compiler directive above
import React from "react";
import { SessionProvider } from "next-auth/react";
export const ClientSessionProvider: typeof SessionProvider = (props) => {
return <SessionProvider {...props} />;
}; ... but it doesn't solve this separate problem (described under Reasoning above).
@balazsorban44 Since that will probably take some time, would you consider merging this small change as a stop-gap? |
Upon revisiting, I think this is a good solution for now and does not introduce problems, even if we want to change APIs in the future. Given |
☕️ Reasoning
Currently, importing "next-auth/react" in a Server Component module (or in a module imported by a Server Component module) will fail with
Uncaught TypeError: React.createContext is not a function
.This is because React Context is unavailable in Server Components.
With this change, "next-auth/react" can be imported without error, and an informative error is thrown if there's an attempt to actually use React Context.
🧢 Checklist
🎫 Affected issues
Related issue: #5647 (this PR doesn't add complete support, but improves support)