Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
Couldn't route to private route with react router v6 and firebase v9

I am using firebase v9 and react-router-dom v6 in my React app. I can detect the onauthstatechanged event but I am unable to route to the private route. Here are my components: Auth:

export const AuthProvider = ({ children }) => {
  const [currentUser, setCurrentUser] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    onAuthStateChanged(auth, user => {
        setCurrentUser(user);
        setLoading(false);
        console.log("un auth",user);
    })
  }, []);

  if(loading) return null;

  return (
    <AuthContext.Provider
      value={{
        currentUser
      }}
    >
      {children}
    </AuthContext.Provider>
  );
};

App:

<AuthProvider>
            <Router>
                <Header />

                <Routes>            
                <Route path="/" element={<Login/>} />
                <Route path="/home" element={
                            <PrivateRoute>
                                <Home />
                            </PrivateRoute>
                        }/>
                </Routes>
            </Router>
    </AuthProvider>

So far, the onauthstatechanged event is triggering. But how can I route to private route?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Are you trying to navigate to a different route when the authstate has changed?

If that's the case, you can use

import {useNavigate} from 'react-router'

const navigate = useNavigate()
onAuthStateChanged = () => {
   ...your code
      navigate('your_private_route')
}

useNavigate is a react hook given by react-router

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda