Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

78
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda