Hello so i'm new to typescript react and currently trying to make applications using typescript and tutorial from youtube.
const RegisterCustomer: React.FC = () => {
const [text, setText] = useState<string>('');
const [email, setEmail] = useState<string>('');
const [username, setUsername] = useState<string>('');
const [password, setPassword] = useState<string>('');
const [isSubmitting, setIsSubmitting] = useState(false);
const { register } = useAuth();
return (
<IonPage>
<Register />
<IonContent className="body">
<IonGrid className="gridinput1">
</IonGrid>
<IonGrid className="gridinput2">
<IonRow>
<IonInput type="email" className="inputEmail" value={email} placeholder="Email" onIonChange={e => setEmail(e.detail.value!)}></IonInput>
</IonRow>
</IonGrid>
<IonGrid className="gridinput3">
<IonRow>
<IonInput type="password" className="inputEmail" value={password} placeholder="Password" onIonChange={e => setPassword(e.detail.value!)}></IonInput>
</IonRow>
</IonGrid>
<IonGrid className="gridinput1">
<IonRow>
<IonButton onClick={async e =>{
e.preventDefault()
const res = await register(email, password);
if(res) {
console.log(res);
}
else {
//handle null response
}
console.log(email, password)
}} className="buttonLogin" expand="block" size="default" color="new">
Register Now
</IonButton>
</IonRow>
</IonGrid>
<p className="loginSeller"><a href="https://www.w3schools.com/">Are you a seller? Login as Seller</a></p>
</IonContent>
</IonPage>
);
};
export default RegisterCustomer;
and this is the authContext
type ButtonProps = {
children: ReactNode;
}
export const useAuth = () =>useContext(AuthContext);
type AuthContextType = {
currentUser: null;
register: (
email: string,
password: string
) => Promise<UserCredential | undefined>;
};
const AuthContext = createContext<Partial<AuthContextType>>({}); // Partial
export default function AuthContextProvider( { children} : ButtonProps){
const [currentUser, setCurrentUser] = useState(null);
function register(email: string, password:string) {
return createUserWithEmailAndPassword(auth, email, password)
}
const value = {
currentUser,
register,
}
return <AuthContext.Provider value={value}>
{children}
</AuthContext.Provider>
}
im trying to get the data from register to firebase but i encounter error " Cannot invoke an object which is possibly 'undefined'. " in register.then in RegisterCustomer page. How do i solve this problem
Partial<AuthContextType>which means all the properties are optional and might be undefined. What would make sense, for your use case, if theregisterfunction was missing from the context? Or if you feel thatregistershould always be present, use a type which keeps that property marked as required. Eg:Partial<Omit<AuthContextType, "register">> & Pick<AuthContextType, "register">.registershould always be part of the context or not. If it can be missing, we will conditionally call it to avoid invoking it if it is undefined. If it cannot be missing, we have to adjust the types you are using for the context to show that it is always present. So, which is the case? Isregisteralways part of the context, or not? (from your code it looks like you expect it to always exist, can you just confirm that is the case?)const AuthContext = createContext<Partial<Omit<AuthContextType, "register">>>({})i got error in RegisterCustomer page, it saysProperty 'register' does not exist on type 'Partial<Omit<AuthContextType, "register">>'