skip to Main Content

Please help me fix this error: When I run the application in nextjs I am getting this message "Error: React Context is unavailable in Server Components"

import { useSession } from "next-auth/react";
import prisma from "@/lib/prisma";
import { ChatbotClient } from "./components/chatbot-client";
import { redirect } from "next/navigation";

interface ChatbotIdPageProps {
  params: {
    chatbotId: string;
  };
}

const ChatbotIdPage = async ({ params }: ChatbotIdPageProps) => {
  const { data: session } = useSession();

  if (!session?.user) {
    redirect("/demo");
  }

  const userId = JSON.stringify(session.user);

  const chatbot = await prisma.chatbot.findUnique({
    where: {
      id: params.chatbotId,
      userId: userId,
    },
  });

  return <ChatbotClient initialData={chatbot} />;
};

export default ChatbotIdPage;

2

Answers


  1. In Next 13, anything that uses React.Context (even your libraries) will need to be a Client component. You can achieve this by having "use client" at the top of your page.

    "use client" 
    import { useSession } from "next-auth/react";
    import prisma from "@/lib/prisma";
    import { ChatbotClient } from "./components/chatbot-client";
    import { redirect } from "next/navigation";
    
    interface ChatbotIdPageProps {
      params: {
        chatbotId: string;
      };
    }
    
    const ChatbotIdPage = async ({ params }: ChatbotIdPageProps) => {
      const { data: session } = useSession();
    
      if (!session?.user) {
        redirect("/demo");
      }
    
      const userId = JSON.stringify(session.user);
    
      const chatbot = await prisma.chatbot.findUnique({
        where: {
          id: params.chatbotId,
          userId: userId,
        },
      });
    
      return <ChatbotClient initialData={chatbot} />;
    };
    
    export default ChatbotIdPage;
    
    Login or Signup to reply.
  2. Instead of useSession which works only in Client Components (with "use client" at the top), use getServerSession since you are in a Server Component:

    import { getServerSession } from "next-auth/next";
    import { authOptions } from "app/api/auth/[...nextauth]"; // ⚠️ Make sure this is the correct path
    import { useSession } from "next-auth/react";
    import prisma from "@/lib/prisma";
    import { ChatbotClient } from "./components/chatbot-client";
    import { redirect } from "next/navigation";
    
    interface ChatbotIdPageProps {
      params: {
        chatbotId: string;
      };
    }
    
    const ChatbotIdPage = async ({ params }: ChatbotIdPageProps) => {
      const session = await getServerSession(authOptions);
    
      if (!session?.user) {
        redirect("/demo");
      }
    
      const userId = JSON.stringify(session.user);
    
      const chatbot = await prisma.chatbot.findUnique({
        where: {
          id: params.chatbotId,
          userId: userId,
        },
      });
    
      return <ChatbotClient initialData={chatbot} />;
    };
    
    export default ChatbotIdPage;
    

    Side note, if you try to add "use client" and stick with useSession, the component function cannot be async, and there you can fetch data only in a useEffect.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search