Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda