Reactjs 财产';然后';不存在于类型';异步动作';Redux工具包
我似乎无法从Reactjs 财产';然后';不存在于类型';异步动作';Redux工具包,reactjs,redux,redux-thunk,redux-toolkit,Reactjs,Redux,Redux Thunk,Redux Toolkit,我似乎无法从Redux工具箱createasynchunk收到Promise函数 我对打字本还相当陌生,我正在努力弄清楚为什么它会给我 类型“AsynchThunkAction”上不存在属性“then”错误,即使如果我删除该类型,承诺也会返回。 这是我的createAsyncThunkf-n export const getStudentByIdRequest = createAsyncThunk<Student, number>( 'student/getStudentById
Redux工具箱createasynchunk
收到Promise
函数
我对打字本还相当陌生,我正在努力弄清楚为什么它会给我
类型“AsynchThunkAction”上不存在属性“then”
错误,即使如果我删除该类型,承诺也会返回。
这是我的createAsyncThunk
f-n
export const getStudentByIdRequest = createAsyncThunk<Student, number>(
'student/getStudentByIdRequest',
async (id, { rejectWithValue }) => {
try {
const { data } = await instance.get(`student/${id}/`)
return data
} catch (err) {
let error: AxiosError = err
if (error) {
return rejectWithValue({
message: `Error. Error code ${error.response?.status}`,
})
}
throw err
}
}
)
错误出现在我尝试调用thunk上的然后
时,您的调度
没有考虑thunk的类型,因此返回类型键入错误。请使用以下文件中所述的门店实际发货类型:
从'@reduxjs/toolkit'导入{configureStore}
从“react redux”导入{useDispatch}
从“./rootReducer”导入rootReducer
常量存储=配置存储({
还原剂:根还原剂
})
导出类型AppDispatch=存储区类型.dispatch
export const useAppDispatch=()=>useDispatch()//导出一个可以重用以解析类型的钩子
然后在组件中使用useAppDispatch
而不是useAppDispatch
。您的dispatch
没有考虑Thunk的类型,因此返回类型键入错误。请使用以下文件中所述的门店实际发货类型:
从'@reduxjs/toolkit'导入{configureStore}
从“react redux”导入{useDispatch}
从“./rootReducer”导入rootReducer
常量存储=配置存储({
还原剂:根还原剂
})
导出类型AppDispatch=存储区类型.dispatch
export const useAppDispatch=()=>useDispatch()//导出一个可以重用以解析类型的钩子
然后在组件中使用useAppDispatch
而不是useAppDispatch
。我这样做了,还必须将中间件:[thunk]更改为const
到中间件:getDefaultMiddleware()
在configureStore
函数中,现在我收到一个错误,上面写着“ReferenceError:无法在初始化之前访问“userReducer”`这个错误正是我首先放弃使用useAppDispatch
的原因,我从未通过从apiConfig.ts
文件到index.ts
,如此处建议的,谢谢您的帮助!我这样做了,我还必须将中间件:[thunk]作为const
更改为中间件:getDefaultMiddleware()
在configureStore
函数中,现在我收到一个错误,上面写着“ReferenceError:无法在初始化之前访问“userReducer”`这个错误正是我首先放弃使用useAppDispatch
的原因,我从未通过从apiConfig.ts
文件到index.ts
,如此处建议的,谢谢您的帮助!
dispatch(getStudentByIdRequest(userId)).then((res) => console.log(res))
import { configureStore } from '@reduxjs/toolkit'
import { useDispatch } from 'react-redux'
import rootReducer from './rootReducer'
const store = configureStore({
reducer: rootReducer
})
export type AppDispatch = typeof store.dispatch
export const useAppDispatch = () => useDispatch<AppDispatch>() // Export a hook that can be reused to resolve types