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

152
Visualizações
Conditionally check value from context and then navigate after login in the same function call in React

In React I'm using the context API to set the user data or the errors returned by the API request:

// context
const [currentUser, setCurrentUser] = useState("")
const [error, setError] = useState("")

async function login(credentials) {
    setError("")
    try {
        ...API request
        const data = await response.json()
        if(data.success) {
            setCurrentUser(data)
        } else {
            setError(data.error)
        }
    } catch (error) {
        setError("Something went wrong. Please try again.")
    }
}

<AuthContext.Provider value={login, currentUser, error}>
    {children}
</AuthContext.Provider>

Then in my component:

//component
const { login, currentUser, error } = useContext(AuthContext)

const onSubmit = async (credentials) => {
    setLoading(true)
    await login(credentials)
    resetForm({})
    setLoading(false)
    !error && navigate("/")
}

return (
    ...
)

Now the onSubmit function allows the user to navigate to / even if there is an error set by the context - this is because (I think) the error is still null by the time it's checked and the component doesn't get the chance to rerender to get the newest value.

What would be the best approach to avoid this behavior? Should I handle the error locally in the component and not the context? If yes, what would be the best way on doing so in my scenario?

Thank you!

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

0

Quick solution

There are of course others ways of doing this, but the one that wouldn't change that much your current structure is to change onSubmit function so you dont do the redirection there:

const onSubmit = async (credentials) => {
    setLoading(true)
    await login(credentials)
    resetForm({})
    setLoading(false)
}

Create an useEffect that would the do the redirection, but based on wether there is a user or not:

useEffect(()=>{
  if(currentUser){
    navigate("/")
  }
},[currentUser])

And yes when you are calling !error && navigate("/") there is no re-render yet, and therefore error is still equal to "", which is why the redirection is happening right away.

Another way

Assuming that the login process is handled in one place (which is the case in most scenarios), there is no need to have login function and error state as part of the context. The context should export just setCurrentUser and currentUser.

<AuthContext.Provider value={currentUser, setCurrentUser}>
    {children}
</AuthContext.Provider>

The login and errors related to login would be handled inside the login page, and it would call setCurrentUser to update the context for other components:

const {currentUser, setCurrentUser} =  useContext(AuthContext)

And you would use the same useEffect as above to make redirection when there is currentUser.

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