Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!