Using context throws an error
Unanswered
arin posted this in #help-forum
arinOP
Hi, I need to do some sort of auth, and I made a context for that (I know that this way is very bad, but it's just for a showcase and won't be really used anywhere anyway)
Here's user-context.tsx
Which is then used in login/page.tsx:
Maybe I'm not an expert, but the same code (with just changed names and missing one function) works perfectly fine.
Anyway the error it throws is
And okay, then just there is async or await somewhere in the code, but... there isn't at all.
Here's user-context.tsx
"use client"
import * as React from "react"
import { UserContextType } from '../../types';
export const UserContext = React.createContext<UserContextType | null>(null);
const UserProvider: React.FC<{children: React.ReactNode}> = ({ children }) => {
const [userId, setUser] = React.useState<string>("");
const loginUser = (id: string) => {
setUser(id);
}
const logoutUser = (id: string) => {
setUser("");
}
return <UserContext.Provider value={{ userId, loginUser, logoutUser }}>{children}</UserContext.Provider>;
}
export default UserProvider;Which is then used in login/page.tsx:
import * as React from 'react';
import { UserContextType } from '@/types';
import { UserContext } from '@/components/providers/user-context';
const LoginPage = ({}: {
children: React.ReactNode
}) => {
const { userId, loginUser, logoutUser } = React.useContext(UserContext) as UserContextType;
loginUser("123");
// ... and the rest of the codeMaybe I'm not an expert, but the same code (with just changed names and missing one function) works perfectly fine.
Anyway the error it throws is
Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding 'use client' to a module that was originally written for the server..And okay, then just there is async or await somewhere in the code, but... there isn't at all.
2 Replies
Satin
I'm not sure, but I think that your
Try adding
page.tsx file is a server-side component by default. Client-side components have to be marked explicitly with 'use client'. Try adding
'use client' to your page.tsx file as well, and see if that fixes it?arinOP
Sorry, i forgot about pasting it here, but it was in this file the whole time. So nope, it's not a solution...
I guess that I will make external server for auth and do a request every page visit..
I guess that I will make external server for auth and do a request every page visit..