Redux Middleware for async actions
- FSA
- Promise based
Usage example
- Actions creators
const githubFetching = const githubError = const githubSuccess =
- Create an action passing all action creators and the api call method
const getUserFromGithub = type : githubFetching githubSuccess githubError payload : axios
- type is an Array with actions creators
- payload must have data property and it must to be a function that returns a Promise
Note: you can pass more properties inside payload and it will be passed to success action.
Basic flux: loginSending -> data -> loginSuccess / loginError
The middleware will call loginSending before apiCall and loginSuccess when promise got resolved, if got an error, loginError will be called.
Reducer / Action creators example (with redux-actions)
- Actions types
const GITHUB_FETCHING = 'modules/Github/FETCHING'const GITHUB_SUCCESS = 'modules/Github/SUCCESS'const GITHUB_ERROR = 'modules/Github/ERROR'
- Reducer
const initialState = fetching : false user : null const reducer = ;
Example without redux-actions
const githubFetching = type : 'GITHUB_FETCHING' const githubError = type : 'GITHUB_ERROR' const githubSuccess = type : 'GITHUB_SUCCESS' payload : payload const initialState = fetching : false user : null const getUserFromGithub = type : githubFetching githubSuccess githubError payload : axios { }