I'm creating a login and signup interface with Firebase, in my Ionic(React) app project, below are the snippet of my code.
interface LoginProps extends RouteComponentProps<any> {}
interface AuthProps {}
const Login: FC<LoginProps> = ({ history }) => {
const handleLogin = useCallback(
async event => {
event.preventDefault();
const { email, password } = event.target.elements;
try {
await Firebase
.auth()
.signInWithEmailAndPassword(email.value, password.value);
history.push("/dashboard");
} catch (error) {
alert(error);
}
},
[history]
);
const { currentUser } = useContext<AuthProps>(AuthContext);
if (currentUser) {
return <Redirect to="/" />;
}
I got this error messages:
[react-scripts] TypeScript error in /Users/matheuslira/Desktop/brasilsul-repasse/src/pages/Login.tsx(43,49):
[react-scripts] Argument of type '{ AuthContext: React.Context; AuthProvider: ({ children }: { children: any; }) => JSX.Element; }' is not assignable to parameter of type 'Context'.
[react-scripts] Type '{ AuthContext: Context; AuthProvider: ({ children }: { children: any; }) => Element; }' is missing the following properties from type 'Context': Provider, Consumer TS2345
the error message point to this part of the code:
const { currentUser } = useContext(AuthContext);
I don't know how to fix this error.
// Edit
AuthContext Code
import React, { useEffect, useState, createContext } from "react"
import Firebase from "./firebase"
export const AuthContext = createContext(null)
export const AuthProvider = ({ children }) => {
const [currentUser, setCurrentUser] = useState(null)
useEffect(() => {
Firebase.auth().onAuthStateChanged(setCurrentUser)
}, [])
return (<AuthContext.Provider value = {{currentUser}} >{children}</AuthContext.Provider>)
}
export default {
AuthContext,
AuthProvider
}
AuthProps
is supposed to be??... where isAuthContext
, I think providing the code from your AuthContext would be helpful – Aaron Saunders