React Redux Toolkit createSyncthunk不给任何回报

发布于 2025-02-09 16:55:59 字数 1397 浏览 3 评论 0原文

我正在使用Redux Toolkit的createAsyncthunkaxios用于异步请求。首先,它有效,但是当我使用SCSS进行STH时,Chrome给我typeerror:无法读取未定义的属性(读取'Map')。我没有改变有关Redux和Thunk的任何内容,但使用Elelector一无所获。另外,界面(起作用)没有给出任何响应。

thunk和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;
        }
    }
})

组件:

const dispatch = useDispatch();
//gets nothing here
const topBanners = useSelector((state) => state.topBanner.pics);
useEffect(() => {
    dispatch(getTopBanners());
  }, [dispatch]);

商店:

const store = configureStore({
  reducer:{
    topBanner:topBannerReducer,
    recommendData:recommendDataReducer
  }
})

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
  }
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅紫色的梦幻 2025-02-16 16:55:59
extraReducers: (builder) => {
    builder
      .addCase(getTopBanners.fulfilled, (state, action) => {
            state.pics = action.payload; 
      });
}

extraReducers: (builder) => {
    builder
      .addCase(getTopBanners.fulfilled, (state, action) => {
            state.pics = action.payload; 
      });
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文