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

77
Visualizações
Proper error handling using Redux Toolkit

Say for example when a login function calls an API and it returns an error because of something like invalid credentials. I have noticed that it still goes through the fulfilled case in the extra reducers part. Should I add an if statement to check if response code is 200 or is there a way for the thunk to go through the rejected case?

extraReducers: builder => {
    builder.addCase(login.pending, (state, action) => {
      state.fetchingError = null;
      state.fetchingUser = true;
    });
    builder.addCase(login.fulfilled, (state, {payload}) => {
      console.log(payload, 'hello?');
      state.user = payload.data.user;
    });
    builder.addCase(login.rejected, (state, action) => {
      state.fetchingUser = false;
      state.fetchingError = action.error;
    });
  },
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use rejectWithValue in createAsyncThunk to customize the reject action.

It also takes an argument which will be "action.payload" in the reject action.

In createAsyncThunk:

    const updateUser = createAsyncThunk(
  'users/update',
  async (userData, { rejectWithValue }) => {
    const { id, ...fields } = userData
    try {
      const response = await userAPI.updateById(id, fields)
      return response.data.user
    } catch (err) {
      // Use `err.response.data` as `action.payload` for a `rejected` action,
      // by explicitly returning it using the `rejectWithValue()` utility
      return rejectWithValue(err.response.data)
    }
  }
)

https://redux-toolkit.js.org/api/createAsyncThunk#handling-thunk-errors

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