在试图在组件中派遣thunk动作时,用打字稿中的react redux遇到某种错误
嗨,大家好,这是我尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在“ store.tsx”中underispatch()中不采用任何
参数,但是您在'gameview.tsx'中通过了一个:
因此,您可以通过在'store.tsx'中添加一个可选的参数来解决此问题,因此它将是:
in 'store.tsx' useDispatch() doesn't take any
argument but you passed one in 'GameView.tsx':
So you can fix this by adding an optional argument to useDispatch in 'store.tsx', so instead it will be :