简体   繁体   English

预期为 0 arguments,但在 redux 和 typescript 中得到 1

[英]Expected 0 arguments, but got 1 in redux with typescript

So I have an async thunk function in redux:所以我在 redux 中有一个异步 thunk function:

export const fetchItem = createAsyncThunk('item/fetchItem', async (id: number) => {
    const res = await fetch(`${apiUrl}/${id}`)
    .then(res => res.json())

    return res        
})

And here is my fetch function in react, it fetches the item by the html id of the event target (an tag):这是我在 React 中获取的 function,它通过事件目标(标签)的 html id 获取项目:

const handleFetch = (e: React.MouseEvent<HTMLAnchorElement>) => {
        useAppDispatch(fetchItem(e.target.id))   
    }

The error is in the fetchItem and it says: Expected 0 arguments, but got 1. I even tried just using:错误在 fetchItem 中,它说:预期为 0 arguments,但得到 1。我什至尝试使用:

useAppDispatch(fetchItem(1))  

And got the same error.并得到了同样的错误。

useAppDispatch looks like this: useAppDispatch 看起来像这样:

import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
import type { RootState, AppDispatch} from "./redux/store";

export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector()

export const useAppDispatch = () => useDispatch<AppDispatch>() 
  1. useAppDispatch is a hook and can be used only directly inside component's body useAppDispatch是一个钩子,只能直接在组件体内使用
  2. useAppDispatch takes 0 arguments, but you pass 1 useAppDispatch取 0 arguments,但你传 1
  3. useAppDispatch returns a dispatch function that you wanted to use useAppDispatch返回您要使用的dispatch function
// inside your component
const dispatch = useAppDispatch()
const handleFetch = (e: React.MouseEvent<HTMLAnchorElement>) => {
  dispatch(fetchItem(e.target.id))   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM