【发布时间】:2021-06-14 10:47:15
【问题描述】:
我想为我从事的项目中的用户创建一个 Redux 切片。我有this code sandbox,不知道为什么在MyButton.tsx文件中的fetchAll调用会出现以下错误:
fetchAll(arg: any): AsyncThunkAction
预期 1 个参数,但得到 0 个。
createAsyncThunk.d.ts(107, 118):未提供“arg”的参数。
我从事的项目中有类似的代码,但没有此错误。我希望这能像在其他类似文件中一样工作。
沙盒中的相关文件:
MyButton.tsx
import React from "react";
import { useDispatch } from "react-redux";
import { fetchAll } from "./redux/usersSlice";
export const MyButton = ({ children }: { children: any }) => {
const dispatch = useDispatch();
return (
<button
onClick={() => {
dispatch(fetchAll()); // I get an error on this fetchAll() call
}}
>
{children}
</button>
);
};
fetchAll的定义
export const fetchAll = createAsyncThunk(
"users/fetchAll",
async (_: any, thunkAPI) => {
const users = await new Promise((resolve, reject) => {
resolve(["a", "b", "c"]);
});
return users;
}
);
更新 1
如果我打电话给fetchAll(null) 而不是fetchAll(),效果很好。
【问题讨论】:
标签: typescript redux react-redux dispatch redux-toolkit