In my slice, I have defined the action to be as follows:
https://github.com/jasonabanico/RedGranite/blob/main/src/Client/RedGranite.Client.Web/src/app/containers/ItemPage/itemPageSlice.ts
export const addItem = createAsyncThunk(
'itemPage/addItem',
async (itemInput: ItemInput) => {
return await itemService
.addItem(itemInput)
.catch((err: any) => {
console.log("Error:", err);
});
},
)
I have also defined useAppDispatch as:
https://github.com/jasonabanico/RedGranite/blob/main/src/Client/RedGranite.Client.Web/src/app/hooks.ts
export const useAppDispatch = () => useDispatch<AppDispatch>();
I use both of the above like so:
https://github.com/jasonabanico/RedGranite/blob/main/src/Client/RedGranite.Client.Web/src/app/containers/ItemPage/addItemPage.tsx
const dispatch = useAppDispatch();
...
dispatch(addItem(itemInput));
I am getting the following error:
TS2345: Argument of type ‘AsyncThunkAction<void | AddItem, ItemInput, AsyncThunkConfig>’ is not assignable to parameter of type ‘AnyAction’.
21 | longDescription
22 | };23 | dispatch(addItem(itemInput));
| ^^^^^^^^^^^^^^^^^^
24 | };
What is wrong with my definitions? Why can’t I use addItem in dispatch?
UPDATE:
Code in repo updated to this, but getting same error.
export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
export const useAppSelector = useSelector.withTypes<RootState>();
UPDATE:
To replicate in Codesandbox.
- Open terminal
- cd src
- cd Client
- cd RedGranite.Client.Web
- npm install
- npm run build
- see build error
2
Answers
According to current documentation it would seem your
useAppDispatch
anduseAppSelector
hooks aren’t quite right.Update your
src/Client/RedGranite.Client.Web/src/app/hooks.ts
file to the current suggestion:It seems you are returning wrong data in your action function.
So I think it should be like this:
Hope this can be helpful.