hey guys I'm currently trying to use hooks and useContext to do authentication
I defined useContext in this file:
import { createContext } from "react";
export const UserContext = createContext(null);
in my Router file I have setup useContext in this way:
export default function Router() {
const [user, setUser] = useState(null);
const value = useMemo(() => ({ user, setUser }), [user, setUser]);
return (
<Switch>
<UserContext.Provider value={value}>
<Route path="/login" component={Login} />
...
</UserContext.Provider>
</Switch>
);
}
Now in my login function I have a problem in my handleSubmit that gets triggered by my form (my backend calls and fetch all work great):
export default function Login() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const { user, setUser } = useContext(UserContext);
const handleSubmit = async (event) => {
event.preventDefault();
const obj = {
Email: email,
Password: password,
};
loginSession(obj);
const session = await getUserData();
setUser(session);
};
in const session = ...
I get my account data
When I setUser(session)
I get the following error:
login.js:25 Uncaught (in promise) TypeError: setUser is not a function
Can anyone help me? I'm trying to figure this out for some hours I read that hooks shouldn't be used everywhere but I guess a function that handles a Submit is fine right?
I also tried to put the code directly in the onSubmit:
onSubmit={async (event) => {
event.preventDefault();
const obj = {
Email: email,
Password: password,
};
loginSession(obj);
const session = await getUserData();
setUser(session);
}}
Prints the same error