What I'm currently doing is:
export type Action =
{ type: 'FOO' }
| { type: 'BAR' }
export type Thunk = (dispatch: Dispatch, getState: GetState) => Action | Thunk
export type Dispatch = ReduxDispatch<Action> & (action: Thunk) => void
but if you dispatch directly on the store
, that won't work without recreating store
:
export type Store = ReduxStore<State, Action>
In general, my thunk solution seems to have other minor problems. Does anyone have a working library definition for redux-thunk
? I can't find one anywhere.
Redux Thunk is a middleware that lets you call action creators that return a function instead of an action object. That function receives the store's dispatch method, which is then used to dispatch regular synchronous actions inside the function's body once the asynchronous operations have been completed.
Redux Async Data Flow Just like with a normal action, we first need to handle a user event in the application, such as a click on a button. Then, we call dispatch() , and pass in something, whether it be a plain action object, a function, or some other value that a middleware can look for.
The best example I've found so far are the ones used in Facebook's own F8 app here.
It's pretty similar to yours:
export type Dispatch = (action: Action | ThunkAction | PromiseAction | Array<Action>) => any;
export type GetState = () => Object;
export type ThunkAction = (dispatch: Dispatch, getState: GetState) => any;
export type PromiseAction = Promise<Action>;
It's worked pretty well for me so far in my project, though I don't dispatch directly on the store anywhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With