I am using context for Auth in my simple typescript app.
My context declaration is in the file
AuthProvider.tsx, which looks like:
import React, { createContext, useState } from 'react';
import auth from '@react-native-firebase/auth';
export interface IAuth {
user,
setUser,
login,
register,
logout
}
export const AuthContext = createContext<IAuth | null>(null);
export const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null);
return (
<AuthContext.Provider
value={{
user: user,
setUser: setUser,
login: async (email, password) => {
try {
await auth().signInWithEmailAndPassword(email, password);
} catch (e) {
console.log(e);
}
},
register: async (email, password) => {
try {
await auth().createUserWithEmailAndPassword(email, password);
} catch (e) {
console.log(e);
}
},
logout: async () => {
try {
await auth().signOut();
} catch (e) {
console.log(e);
}
},
}}>
{children}
</AuthContext.Provider>
);
};
This is how I am using it elsewhere:
const { user, setUser } = useContext(AuthContext);
But Typescript throws 2 errors on this lien:
- Property 'user' does not exist on type 'IAuth | null'
- Property 'setUser' does not exist on type 'IAuth | null'
What can I do to bypass it?