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

158
Views
I keep redirecting to '/chats' using history.push() I tried to make the user null but stuck

I was watching a tutorial and I got stuck on this part wherein I keep on redirecting to '/chat'. I set the user to null but I keep on redirecting to '/chats'. I tried to re-watch the vid, but I keep getting stuck on this part.

AuthContext.js

import React, { useContext, useEffect, useState } from 'react'
import { useHistory } from 'react-router-dom'
import { auth } from '../firebase'

const AuthContext = React.createContext()

export const useAuth = () => useContext(AuthContext)

export const AuthProvider = ({ children }) => {
  const [loading, setLoading] = useState(true)
  const [user, setUser] = useState(null)
  const history = useHistory()

  useEffect(() => {
    auth.onAuthStateChanged((user) => {
      setUser(user)
      setLoading(false)
      if (user) {
        history.push('/chats')
      }
    })
  }, [user, history])

  const value = { user }
  return (
    <AuthContext.Provider value={value}>
      {!loading && children}
    </AuthContext.Provider>
  )
}

App.js

import 'firebase/app'
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
import Chats from './components/Chats'
import Login from './components/Login'
import { AuthProvider } from './context/AuthContext'

function App() {
  return (
    <>
      <Router>
        <AuthProvider>
          <Switch>
            <Route path="/chats">
              <Chats />
            </Route>
            <Route path="/">
              <Login />
            </Route>
          </Switch>
        </AuthProvider>
      </Router>
    </>
  )
}

export default App

about 4 years ago · Juan Pablo Isaza
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!