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

133
Visualizações
Condition is checked with the initial value Redux state

I try to get the user information from the API and check that if it is isAuthenticated === true go to the profile page, otherwise to the login page,But since JavaScript does not wait for a response, it executes the code one after the other and checks the condition with the same initialization as false.

What should I do to wait for a response from the API and change the value of isAuthenticated and userInfo and then check the following condition?

    const userLoad = useSelector(state => state.userLoad)
    const {error, loading, isAuthenticated, userInfo} = userLoad

    useEffect(() => {
        if (isAuthenticated) {
            dispatch(getUserDetails('profile'))
        } else {
            history.push('/login')
        }
    }, [dispatch, history, isAuthenticated, userInfo])

I created a userLoadReducer which is as follows:

export const userLoadReducer = (state = {}, action) => {
    const {type, payload} = action;

    switch (type) {

        case USER_LOADED_REQUEST:
            return {
                ...state,
                loading: true
            }

        case USER_LOADED_SUCCESS:
            return {
                ...state,
                loading: false,
                isAuthenticated: true,
                userInfo: payload.user,
            }

        case USER_LOADED_FAIL:
            return {
                ...state,
                loading: false,
                isAuthenticated: false,
                userInfo: null
            }

        default:
            return state
    }
}

And its load_user action is as follows:

export const load_user = () => async dispatch => {
    const config = {
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
    };

    try {
        dispatch({type: USER_LOADED_REQUEST});

        const response = await axios.get(`/auth/user/`, config);

        if (response.status === 200) {
            dispatch({
                type: USER_LOADED_SUCCESS,
                payload: response.data
            });
        } else {
            dispatch({
                type: USER_LOADED_FAIL
            });
        }

    } catch (err) {
        dispatch({
            type: USER_LOADED_FAIL
        });
    }
};

inspect: console.log()

| Order    | Result
| -------- | ---------------------------------------------------- |
| First    | {loading: false, isAuthenticated: false, userInfo: null} |                       
| Second   | {loading: true, isAuthenticated: false, userInfo: null} |
| third    | {loading: false, isAuthenticated: true, userInfo: {…}} |                             
about 4 years ago · Juan Pablo Isaza
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