问题
I am getting this error in a big react native app. Actually I am running this on expo. Can anyone see through and suggest a solution.Please Help "undefined is not an object evaluating '_ usecontext.user'"
import React, { useState } from "react";
import { StyleSheet, Image } from "react-native";
import * as Yup from "yup";
import Screen from "../components/Screen";
import {
ErrorMessage,
Form,
FormField,
SubmitButton,
} from "../components/forms";
import authApi from "../api/auth";
import useAuth from "../auth/useAuth";
const validationSchema = Yup.object().shape({
email: Yup.string().required().email().label("Email"),
password: Yup.string().required().min(4).label("Password"),
});
function LoginScreen(props) {
const auth = useAuth();
const [loginFailed, setLoginFailed] = useState(false);
const handleSubmit = async ({ email, password }) => {
const result = await authApi.login(email, password);
if (!result.ok) return setLoginFailed(true);
setLoginFailed(false);
auth.logIn(result.data);
};
//not full code here
}
#auth.js (AuthApi) #
mport client from "./client";
const login = (email, password) => client.post("/auth", { email, password });
export default {
login,
};
#useAuth#
import { useContext } from "react";
import jwtDecode from "jwt-decode";
import AuthContext from "./context";
import authStorage from "./storage";
export default useAuth = () => {
const { user, setUser } = useContext(AuthContext);
const logIn = (authToken) => {
const user = jwtDecode(authToken);
setUser(user);
authStorage.storeToken(authToken);
};
const logOut = () => {
setUser(null);
authStorage.removeToken();
};
return { user, logIn, logOut };
};
#AuthContext#
import React from "react";
const AuthContext = React.createContext();
export default AuthContext;
Tell me if you need more code. Please Help
来源:https://stackoverflow.com/questions/63266275/undefined-is-not-an-object-evaluating-usecontext-user