带有redux和tyscript不起作用的功能
我正在尝试使用RECT中的Redux和TypeScript派遣功能,但是我会遇到此错误:
Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'
功能1:
const postProducts = (origin: string) => {
return (dispatch: Dispatch) => {
dispatch(initProducts(origin))
}
}
功能2:
export const initProducts = (origin: string) => {
return (dispatch: Dispatch) => {
if (origin !== 'load') {
dispatch(setToastify(errorMsg))
}
}
}
功能3:
export const setToastify = (toastifyDetails: string, open = true) => {
return {
type: ActionType.SET_TOASTIFY,
toastify: toastifyDetails,
open: open
}
}
PS我知道现在我应该使用Redux Toolkit,但此项目仅用于教育目的。
I'm trying to dispatch a function using Redux and Typescript in React, but I'm getting this error:
Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'
Function 1:
const postProducts = (origin: string) => {
return (dispatch: Dispatch) => {
dispatch(initProducts(origin))
}
}
Function 2:
export const initProducts = (origin: string) => {
return (dispatch: Dispatch) => {
if (origin !== 'load') {
dispatch(setToastify(errorMsg))
}
}
}
Function 3:
export const setToastify = (toastifyDetails: string, open = true) => {
return {
type: ActionType.SET_TOASTIFY,
toastify: toastifyDetails,
open: open
}
}
P.S. I know nowadays I should be using Redux Toolkit, but this project is just for educational purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
dispatch
函数类型不知道您具有thunk
中间件活动。请关注 typescript快速启动教程如何以某种方式查看如何以某种方式设置您的商店这些类型正确。您最终将获得某种
appDisPatch
用于使用的类型,而不是普通dispatch
在此处类型。Your
dispatch
functions types are not aware that you have thethunk
middleware active.Please follow the TypeScript Quick Start tutorial to see how to set up your store in a way that the types are in place correctly. You will end up with some kind of
AppDispatch
type to use instead of the plainDispatch
type here.对于每个操作,您应具有定义的动作类型。对于
settoastify
:您的
settoastify
应该像这样:您可能有一个以上的操作。定义类型:
传话
然后在还原器中使用它:
for each action you should have an action type defined. for
setToastify
:your
setToastify
should be like this:you might have more than one action. Define type:
herAction
Then use this in your reducer: