question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createAsyncThunk type with correct dispatch not working

See original GitHub issue

I found multiple errors regarding this, but everyone’s solution was that they used the dispatch function from react-redux. I’m using the one from my store, but I still get the Argument of type 'AsyncThunkAction<...> is not assignable to parameter of type 'AnyAction'. Property 'type' is missing in type

I am calling the createAsyncThunk function

export const connectToDevice = createAsyncThunk('name', async () => {async stuff being done here and returning a promise})

then i’m calling the createSlice and adding all 3 states under the extraReducers key of the connectToDevice action;

const s = createSlice({
name: '...',
initialState: initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(connectToDevice.pending, (state: State, action) => {
      ...
    });
...
}
});

export default s.reducer;

and then in my store:

const store = configureStore({
  reducer: {
    s: s,
  },
});

export default store;
export type AppDispatch = typeof store.dispatch;

and then:

import {useDispatch} from 'react-redux';

import {AppDispatch, RootState} from '.';

export const useAppDispatch = () => useDispatch<AppDispatch>();

I’m new to react, redux but i guess after this point the useAppDispatch() should know about the thunk middleware…

so in my component:

const dispatch: AppDispatch = useAppDispatch();
dispatch(connectToDevice());

Still… I’ve got the error. What am I doing wrong?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Feb 27, 2021

Hmm. That looks like it should be correct. Can you put up a CodeSandbox that reproduces this issue?

0reactions
Golyo88commented, Mar 1, 2021

You are right @phryneas! Thanks a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

createAsyncThunk type infer not working correctly in 1.6.0 with ...
In 1.5.1 work fine, in 1.6.0 not. If explicitly typed, everything ok. Argument of type '() => Promise ' is not assignable to...
Read more >
Redux-Toolkit with Typescript. Cannot dispatch action created ...
I was not importing the action correctly and it picked up my reducer object instead. Hope this helps anyone experiencing the same!
Read more >
createAsyncThunk - Redux Toolkit
createAsyncThunk. Overview​. A function that accepts a Redux action type string and a callback function that should return a promise.
Read more >
Chaining Asynchronous Dispatches in Redux Toolkit - YouTube
In this video I cover how we can correctly dispatch one action after another, where the second dispatch is informed by the state...
Read more >
createAsyncThunk in Redux-Toolkit - In Plain English
It generates promise lifecycle action types based on the action type prefix ... If condition is false *dispatchConditionRejection *will not ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found