类型错误:无法读取未定义的属性“类型”(redux 工具包)

2024-03-13

我正在尝试使用 redux 工具包获取一些数据,但它不起作用。我只是不断收到错误TypeError: Cannot read property 'type' of undefined。我正确设置了商店,因为我有其他减速器工作正常。但是当我尝试异步或获取数据时,我遇到了这个问题

Error:

App.js:

代码停止于const actionResult = await dispath(getLiveContest())之后它不会控制台记录任何内容。

  const dispatch = useDispatch();

  useEffect(() => {
    const fetchLiveContest = async () => {
      try {
        console.log(1);
        const actionResult = await dispatch(getLiveContest());
        console.log(2);
        const liveContest = unwrapResult(actionResult);
        console.log(liveContest);
      } catch (error) {
        console.log("Failed to fetch live contest: ", error);
      }
    };

    fetchLiveContest();
  }, []);

获取LiveContest():

这是该函数的代码。我尝试过了return {name: 'lala'}它仍然给我类型错误

export const getLiveContest = createAsyncThunk(
  "contests/fetchLive",
  async (params, thunkAPI) => {
    console.log(thunkAPI, "thunkAPI");
    console.log(params);
    const liveContest = await axios ...

    return liveContest;
  }
);

幻灯片代码:

export const liveContestSlide = createSlice({
  name: "live",
  initialState: {
    contest: [],
    loading: "idle",
  },
  reducers: {},
  extraReducers: {
    // Add reducers for additional action types here, and handle loading state as needed
    [getLiveContest.fulfilled]: (state, action) => {
      // Add contest to the state array
      state.contest.push(action.payload);
    },
  },
});

我遵循了 redux 工具包文档。我还检查了 stackoverflow 上的其他问题,但仍然无法修复错误,请帮忙


我只是改变import getLiveContest from "./contestSlice"; to import { getLiveContest } from "./contestSlice";它起作用了,结果我只是错误地导入了函数

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类型错误:无法读取未定义的属性“类型”(redux 工具包) 的相关文章

随机推荐