undefined is not an object evaluating '_ usecontext.user'

廉价感情. 提交于 2021-02-11 14:53:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!