Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are you supposed to flowtype redux thunk?

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.

like image 874
faceyspacey.com Avatar asked Jan 22 '17 15:01

faceyspacey.com


People also ask

How do you dispatch actions in redux-thunk?

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.

How do you handle async actions in Redux?

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.


1 Answers

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.

like image 81
apollo9981 Avatar answered Oct 03 '22 11:10

apollo9981