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

202
Visualizações
Redux states don't update inside the firebase auth onAuthStateChanged callback

I use a redux state inside the firebase auth onAuthStateChanged callback like this

...
const authUser  = useSelector(state => state.authUser)
console.log(authUser.status)
useEffect(() => {
  const unsubscribe = onAuthStateChanged(auth, () => {
     console.log(authUser.status)
     if (authUser.status === 'loading') {
       dispatch(updateStatus('loaded'))
  }
  return () => unsubscribe()
}, []);
...

First it logged

loading

loading

then after I signed in the auth state changed and it logged

loaded

loading

The initial state of status is loading. Every time the auth state changes it will call the callback function. The value of status inside the callback is always loading even that its value in the store has changed to loaded. Why doesn't its value in the callback change?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

After reading this explanation from Dan I knew that states, props will never change inside a closure. To achieve the logic I wanted, just rewrite the update logic inside createSlice

...
    updateStatus: (state, action) => {
      if (state.auth.status === 'loading') {
        state.auth.status = 'loaded'
      }
    }
...
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