![](/img/trans.png)
[英]Dispatching a Redux Thunk action outside of a React component with TypeScript
[英]Dispatching Thunk to the store with Typescript
在我的主要store.ts中,我有這個:
const store = createStore( rootReducer, composeWithDevTools(applyMiddleware(thunk)) ); store.dispatch(fetchUser());
因此,在第一個渲染器上,它向fetchUser調度並執行操作,以查看他/她是否可以看到內容。
這是重擊:
export const fetchUser = (): ThunkAction< void, TRootState, null, Action > => async dispatch => { const response = await axios.get(GET_USER_INFORMATION); dispatch({ type: FETCH_USER, payload: response.data }); };
但是Typescript拋出錯誤:
src/store/index.ts (14,16): Argument of type 'ThunkAction<void, { userReducer: any; }, null, Action<any>>' is not assignable to parameter of type 'AnyAction'. Property 'type' is missing in type 'ThunkAction<void, { userReducer: any; }, null, Action<any>>'.
store / index.ts第14行:
store.dispatch(fetchUser())
老實說,我迷路了。 我該如何解決? 我不想在thunk中輸入“ any”。
store.dispatch僅接受一個動作,而不接受功能。 但重擊將其覆蓋。 您需要指定轉換類型。
嘗試這個
composeWithDevTools(applyMiddleware(thunk as ThunkMiddleware<RootState, RootAction>))
另外,請查看此官方文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.