Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Firebase useAuth() context in App.js is undefined?

I have a AuthProvider and useAuth method that returns useContext(AuthContext) inside my App.js root file, however, if I console.log the return value of useAuth() in App.js, it is undefined, why?? In any other child component like Loginpage, there is an authContext displayed..

my App.js:

import { AuthProvider, useAuth } from './providers/AuthProvider'
function App() {
    const auth = useAuth()
    console.log({ auth }) //undefined??

AuthProvider.js:

import React, { useContext, useState, useEffect } from 'react'
import { app, auth, firestore, storage } from '../firebase'

const AuthContext = React.createContext()

export function useAuth() {
    return useContext(AuthContext)
}
export function AuthProvider({ children }) {
    const [currentUser, setCurrentUser] = useState(undefined)
    const [loginError, setLoginError] = useState(null)

    const login = async (email, password) => {
        try {
            return auth.signInWithEmailAndPassword(email, password)
        } catch (error) {
            setLoginError(error)
        }
    }

    

    const logout = () => auth.signOut()

    useEffect(() => {
        const unsubscribe = auth.onAuthStateChanged(user => {
            setCurrentUser(user)
        })
        return unsubscribe
    }, [])

    const value = {
        loginError,
        login,
        signup,
        logout,
        currentUser,
        app,
        firestore,
        storage,
        auth,
    }

    return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since you import AuthProvider in same file of App it gives a clue that you didn't use the Context API correctly.

Context values are available for Context-Consumers.

Context-Consumers are children of Context-Provider, therefore your App component have to be a child of <AuthContext.Provider>:

<AuthProvider>
  <App />
</AuthProvider>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!