IndexedDB 在React组件+ typescript中调度时输入错误

2guxujil  于 2023-08-01  发布在  IndexedDB
关注(0)|答案(1)|浏览(151)

我在handleSubmit上收到类型错误分派(createNote(data))但是,如果您关闭页面上的错误并从表单送出数据,则数据会成功储存在indexeddb中
错误代码:

**Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'
(alias) createNote(data: Note): (dispatch: Dispatch<AnyAction>) => void
import createNote**

字符串
与此同时,切片本身没有错误。“
我试过将调度更改为任意,但没有成功

export const createNote = (data: Note) => (dispatch: any) => {
     console log(data);
     dispatch(createNoteReq());

     try {
         addNote(data);
         dispatch(noteCreate(data));
     } catch (error: any) {
         dispatch(notesReqFailed(error.message));
     }
};
export const createNote = (data: Note) => (dispatch: Dispatch<AnyAction>) => {
     console log(data);
     dispatch(createNoteReq());

     try {
         addNote(data);
         dispatch(noteCreate(data));
     } catch (error: any) {
         dispatch(notesReqFailed(error.message));
     }
};

的数据

UPD:在曲面上找到解决方案

需要使用useAppDispatch与useDispatch,以及使用useAppSelector与useSelector

7eumitmz

7eumitmz1#

如果你使用的是redux的dispatch,你应该从redux store中导出类型并将其导入到你的组件中

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

export 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

个字符

相关问题