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.

When react-redux@8 is installed, type is not normally found in dispatch.

See original GitHub issue

What version of React, ReactDOM/React Native, Redux, and React Redux are you using?

  • React: 17.0.2
  • ReactDOM/React Native: 17.0.2 / 0.68.1
  • Redux: 4.2.0
  • React Redux: 8.0.1

What is the current behavior?

TS2345: Argument of type ‘AsyncThunkAction<TicketEntity[], void, {}>’ is not assignable to parameter of type ‘AnyAction’. Property ‘type’ is missing in type ‘AsyncThunkAction<TicketEntity[], void, {}>’ but required in type ‘AnyAction’.

const dispatch = useDispatch();
...
useEffect(()=>{
  dispatch(anyAction()) ; // error
},[dispatch])


### What is the expected behavior?

dispatch type changed before version

// v7
const dispatch: Dispatch
<AsyncThunkAction<ILoadNoticeListReturn, boolean | QueryDocumentSnapshot<INotice> | undefined, {}>>(action: AsyncThunkAction<...>) => AsyncThunkAction<...>

// v8
const dispatch: Dispatch
<AnyAction>(action: AnyAction) => AnyAction

### Which browser and OS are affected by this issue?

_No response_

### Did this work in previous versions of React Redux?

- [X] Yes

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Kir93commented, May 9, 2022

@phryneas oh! Thank you for the reply. Hope you have a nice day 😃

1reaction
phryneascommented, May 9, 2022

This was actually a bug in the past. since the default dispatch type does not take the redux-thunk middleware into consideration and so always returned a wrong return value from the dispatch call. In short, it was possible to dispatch anything that was not a valid action.

To have redux-thunk’s typings for a store with thunk, you need to set up correctly typed hooks:

// store.ts

import { configureStore } from '@reduxjs/toolkit'
// ...

const store = configureStore({
  reducer: {
    posts: postsReducer,
    comments: commentsReducer,
    users: usersReducer
  }
})

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch


// hooks.ts

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

// Use throughout your app instead of plain `useDispatch` and `useSelector`
export const useAppDispatch = () => useDispatch<AppDispatch>()
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
Read more comments on GitHub >

github_iconTop Results From Across the Web

Type errors after upgrading to 1.8.1 and Redux 8 ... - GitHub
Argument of type '(dispatch: AppDispatch) => Promise<void>' is not assignable to parameter of type 'AnyAction'. Property 'type' is missing in type ...
Read more >
React 18, React Redux 8, and TypeScript: What you need to ...
Learn about Redux 8, a state management library for React, and how it works with React 18 and it's TypeScript codebase conversion.
Read more >
Connect | React Redux
The connect() function connects a React component to a Redux store. It provides its connected component with the pieces of the data it...
Read more >
Redux Toolkit - Argument of type 'AsyncThunkAction<>' is not ...
This is my very basic action: export const getAllPages = createAsyncThunk<GETALLPAGESACTION, string, { dispatch: AppDispatch; state ...
Read more >
import provider from 'react-redux' error - You.com - You.com
Attempted import error: 'Provider' is not exported from 'react-redux' ... It just calls dispatch normally, it can also use mapDispatchToProps to bind such ......
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