Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6314

Getting TS Error: Property 'user' does not exist on type 'IAuth | null'

$
0
0

I am using context for Auth in my simple typescript app.My context declaration is in the fileAuthProvider.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:

  1. Property 'user' does not exist on type 'IAuth | null'
  2. Property 'setUser' does not exist on type 'IAuth | null'

What can I do to bypass it?


Viewing all articles
Browse latest Browse all 6314

Trending Articles