I am using Redux Toolkit's createAsyncThunk
and Axios
for async requests. At first, it works, but when I was doing sth with scss, the chrome gives me that TypeError: Cannot read properties of undefined (reading 'map')
. I didn't change anything about redux and thunk but useSelector gets nothing. Also, the interface(it works) didn't give any response.
thunk and slice:
export const getTopBanners = createAsyncThunk(
'home/getTopBanners',
async ()=>{
console.log('hello?');
const response = await axiosGetTopBanner();
console.log('res:'+response);
return response.banners
}
)
export const topBannerSlice = createSlice({
name:'topBanner',
initialState:{
pics:[],
isLoading:false
},
extraReducers:{
[getTopBanners.pending]:(state)=>{
state.isLoading = true;
},
[getTopBanners.fulfilled]:(state,action)=>{
state.pics = action.payload;
state.isLoading = false;
},
[getTopBanners.rejected]:(state)=>{
state.isLoading = false;
}
}
})
component:
const dispatch = useDispatch();
//gets nothing here
const topBanners = useSelector((state) => state.topBanner.pics);
useEffect(() => {
dispatch(getTopBanners());
}, [dispatch]);
store:
const store = configureStore({
reducer:{
topBanner:topBannerReducer,
recommendData:recommendDataReducer
}
})