在试图在组件中派遣thunk动作时,用打字稿中的react redux遇到某种错误

发布于 2025-01-22 13:41:56 字数 3388 浏览 2 评论 0原文

嗨,大家好,这是我尝试使用Redux-Toolkit的React-Typecript并遇到错误的第一个项目:

类型'appthunk'的参数不能分配给类型“ AnyAction”的参数。类型为“ appthunk”中缺少属性“类型”,但类型为“ AnyAction”中需要。

基本上,我只想从CATS Public API中获取数据,但这似乎不起作用,我真的会为您提供一些帮助。

src/app/rootreducer.tsx

import { combineReducers } from "@reduxjs/toolkit";
import catsReducer from "../features/cat/catsSlice";

const rootReducer = combineReducers({
  cats: catsReducer,
});

export type RootState = ReturnType<typeof rootReducer>;

export default rootReducer;

src/app/store.tsx

import { configureStore, Action } from "@reduxjs/toolkit";
import { useDispatch } from "react-redux";
import { ThunkAction } from "redux-thunk";

import rootReducer, { RootState } from "./rootReducer";

const store = configureStore({
  reducer: rootReducer,
});

export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = () => useDispatch();
export type AppThunk = ThunkAction<void, RootState, unknown, Action>;

export default store;

src/fromate/cat/catsslice.tsx

import axios from "axios";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";

import { AppThunk } from "../../app/store";
import { RootState } from "../../app/rootReducer";

export interface CatsState {
  cats: Array<Cat>;
  isLoading: boolean;
  error: CatsError;
}

export interface Cat {
  id: string;
  categories?: Array<any>;
  breeds: Array<any>;
  url: string;
  width: number;
  height: number;
}

export interface CatsError {
  message: string;
}

export const initialState: CatsState = {
  cats: [],
  isLoading: false,
  error: { message: "" },
};

export const catsSlice = createSlice({
  name: "cat",
  initialState,
  reducers: {
    setLoading: (state, { payload }: PayloadAction<boolean>) => {
      state.isLoading = payload;
    },
    setCatsSuccess: (state, { payload }: PayloadAction<Array<Cat>>) => {
      state.cats = payload;
    },
    setCatsFailed: (state, { payload }: PayloadAction<CatsError>) => {
      state.error = payload;
    },
  },
});

export const { setCatsSuccess, setCatsFailed, setLoading } = catsSlice.actions;

export const getCats = (): AppThunk => async (dispatch) => {
  try {
    dispatch(setLoading(true));
    const catsResponse = await axios.get(
      "https://api.thecatapi.com/v1/images/search?limit=8&size=small&order=random"
    );
    dispatch(setCatsSuccess(catsResponse.data));
  } catch (error) {
    dispatch(setCatsFailed({ message: "An Error occurred" }));
  } finally {
    dispatch(setLoading(false));
  }
};

export const catsSelector = (state: RootState) => state.cats;
export default catsSlice.reducer;

src/components/game/gameview.tsx,

import React, { useEffect } from "react";
import { getCats, catsSelector, Cat } from "../../features/cat/catsSlice";
import { useSelector, useDispatch } from "react-redux";

const GameView: React.FC = (): JSX.Element => {
  const dispatch = useDispatch();
  const { cats, isLoading, error } = useSelector(catsSelector);

  useEffect(() => {
    dispatch(getCats());
  }, [dispatch]);

  return <>GameView</>;
};

export default GameView;

因此您可以看到我在useffect Hook中呼叫getCats()函数,但是它被带有错误。

任何帮助都将被应用!提前致谢!

So hi guys, this is my first project where I try to use react-typescript with redux-toolkit and am getting an error:

Argument of type 'AppThunk' is not assignable to parameter of type 'AnyAction'. Property 'type' is missing in type 'AppThunk' but required in type 'AnyAction'.

Basically I just want to get data from cats public API, but it doesn't seem to work and I would really appriciate some help.

src/app/rootReducer.tsx

import { combineReducers } from "@reduxjs/toolkit";
import catsReducer from "../features/cat/catsSlice";

const rootReducer = combineReducers({
  cats: catsReducer,
});

export type RootState = ReturnType<typeof rootReducer>;

export default rootReducer;

src/app/store.tsx

import { configureStore, Action } from "@reduxjs/toolkit";
import { useDispatch } from "react-redux";
import { ThunkAction } from "redux-thunk";

import rootReducer, { RootState } from "./rootReducer";

const store = configureStore({
  reducer: rootReducer,
});

export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = () => useDispatch();
export type AppThunk = ThunkAction<void, RootState, unknown, Action>;

export default store;

src/features/cat/catsSlice.tsx

import axios from "axios";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";

import { AppThunk } from "../../app/store";
import { RootState } from "../../app/rootReducer";

export interface CatsState {
  cats: Array<Cat>;
  isLoading: boolean;
  error: CatsError;
}

export interface Cat {
  id: string;
  categories?: Array<any>;
  breeds: Array<any>;
  url: string;
  width: number;
  height: number;
}

export interface CatsError {
  message: string;
}

export const initialState: CatsState = {
  cats: [],
  isLoading: false,
  error: { message: "" },
};

export const catsSlice = createSlice({
  name: "cat",
  initialState,
  reducers: {
    setLoading: (state, { payload }: PayloadAction<boolean>) => {
      state.isLoading = payload;
    },
    setCatsSuccess: (state, { payload }: PayloadAction<Array<Cat>>) => {
      state.cats = payload;
    },
    setCatsFailed: (state, { payload }: PayloadAction<CatsError>) => {
      state.error = payload;
    },
  },
});

export const { setCatsSuccess, setCatsFailed, setLoading } = catsSlice.actions;

export const getCats = (): AppThunk => async (dispatch) => {
  try {
    dispatch(setLoading(true));
    const catsResponse = await axios.get(
      "https://api.thecatapi.com/v1/images/search?limit=8&size=small&order=random"
    );
    dispatch(setCatsSuccess(catsResponse.data));
  } catch (error) {
    dispatch(setCatsFailed({ message: "An Error occurred" }));
  } finally {
    dispatch(setLoading(false));
  }
};

export const catsSelector = (state: RootState) => state.cats;
export default catsSlice.reducer;

src/components/game/GameView.tsx

import React, { useEffect } from "react";
import { getCats, catsSelector, Cat } from "../../features/cat/catsSlice";
import { useSelector, useDispatch } from "react-redux";

const GameView: React.FC = (): JSX.Element => {
  const dispatch = useDispatch();
  const { cats, isLoading, error } = useSelector(catsSelector);

  useEffect(() => {
    dispatch(getCats());
  }, [dispatch]);

  return <>GameView</>;
};

export default GameView;

So as you can see I call the getCats() function in the useEffect hook, but it gets underlined with an error.

Any help would be appriciated! Thanks in advance!

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

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

发布评论

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

评论(1

や三分注定 2025-01-29 13:41:56

在“ store.tsx”中underispatch()中不采用任何

 export const useAppDispatch = () => useDispatch();

参数,但是您在'gameview.tsx'中通过了一个:

 dispatch(getCats());

因此,您可以通过在'store.tsx'中添加一个可选的参数来解决此问题,因此它将是:

export const useAppDispatch: (arg?:unknown) => AppDispatch = useDispatch

in 'store.tsx' useDispatch() doesn't take any

 export const useAppDispatch = () => useDispatch();

argument but you passed one in 'GameView.tsx':

 dispatch(getCats());

So you can fix this by adding an optional argument to useDispatch in 'store.tsx', so instead it will be :

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