我在我的 react-native(typescript) redux 项目中使用 redux-thunk。我正在使用 thunkWithExtraArgument Helper 来提供我的 apollo 客户端变量的引用,因为我得到了返回函数的三个参数,即
...
return async (dispatch, getState, client) =>
...
// and with typescript
import { NormalizedCacheObject } from 'apollo-cache-inmemory'
import ApolloClient from 'apollo-client'
import { Dispatch } from 'redux'
import { ReduxStore } from '../interfaces/redux.ts';
...
return async (dispatch: Dispatch, getState: () => ReduxStore, client: ApolloClient<NormalizedCacheObject>): Promise<void>) => {
...
}
我必须在任何地方都输入很多代码,所以有什么解决方案吗?
目前,我正在做的是:
import { NormalizedCacheObject } from 'apollo-cache-inmemory'
import ApolloClient from 'apollo-client'
import { Dispatch } from 'redux'
import { ReduxStore } from '../interfaces/redux.ts'
// actually i outsources both GetState and Client types
type GetState = () => ReduxStore
// where ReduxStore is custom defined interface
type Client = ApolloClient<NormalizedCacheObject>
const fetchSomeData = (id: string) => {
return async (dispatch: Dispatch, getState: GetState, client: Client): Promise<void>) => {
...
}
}
我尝试过并且需要的是
...
export type Thunk = (dispatch: Dispatch, getState: () => ReduxStore, client: ApolloClient<NormalizedCacheObject>): Promise<void>
...
// action creator
const fethcSomeData = (id: string) => {
return async (dispatch, getState, client): Thunk => {
...
}
}}