'Changing user's info in Context api using React useState Hooks
I am trying to use Context API to store the user's info after he is logged in, but the setAuth, when I try to set the data says that function "setAuth is not a function"
import { createContext, useState } from "react";
const AuthContext = createContext({});
export const AuthProvider = ({ children }) => {
const [auth, setAuth] = useState(null);
return (
<AuthContext.Provider value={(auth, setAuth)}>
{children}
</AuthContext.Provider>
);
};
export default AuthContext;
Login view
const { user, setAuth } = useContext(AuthContext);
//formik on submit function
onSubmit: (values) => {
setAuth(values); // setAuth is not a function
Solution 1:[1]
<AuthContext.Provider value={auth, setAuth}>
{children}
</AuthContext.Provider>
Remove the brackets.
Also, check if LoginView is inside AuthProvider
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dhruv tailor |
