Вызов функции rejectWithValue не вызывает блок обработки ошибки в экстраредюсере.
import {createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import {TypeResponse, TypeStateFilm } from "../../types/TypesFilm";
const initialState: TypeStateFilm = {
Search: [],
Loading: false,
totalResults: 0,
Response: true,
}
export const getFilmsAsync = createAsyncThunk(
'counter/fetchCount',
async (filmName: string, {rejectWithValue}) => {
try{
const response = await fetch(`http://www.omdbapi.c2om/?apikey=64405bd2&s=${filmName}`);
const data = response.json() as Promise<TypeResponse>;
if(!response.ok)
rejectWithValue("Ошибка загрузки фильмов")
return data;
}
catch(e) {
rejectWithValue(e);
return {...initialState, Response: false};
}
}
);
export const filmsSlice = createSlice({
name: "films",
initialState,
reducers: {
},
extraReducers: (builder) => {
builder
.addCase(getFilmsAsync.pending, (state) => {
state.Loading = true;
})
.addCase(getFilmsAsync.fulfilled, (state, action) => {
state.Search = action.payload.Search;
state.Response = true;
state.Loading = false;
})
.addCase(getFilmsAsync.rejected, (state) => {
state.Loading = false;
state.Response = false;
})
}
});
export default filmsSlice.reducer;