Answer the question
In order to leave comments, you need to log in
How to make actions asynchronous in Redux + AngularJS?
I got one project. I'm trying to upgrade it and decided to add redux to it.
Stuck at one point. I get the error "Actions must be plain objects. Use custom middleware for async actions". I understand what she means. But I don’t understand why it occurs.
This is how I register the store in the application config:
$provide.factory('injectMiddleware', ($http) => {
return thunk.withExtraArgument({ $http })
})
$ngReduxProvider.createStoreWith(
store,
['injectMiddleware'],
process.env.DEBUG
&& window.__REDUX_DEVTOOLS_EXTENSION__
&& [window.__REDUX_DEVTOOLS_EXTENSION__()]
)
const initialState = {
current: null
}
export const UsersReducer = (state = initialState, action) => {
switch (action.type) {
case 'SET_CURRENT':
return {
...state,
...action.payload
}
default:
return state
}
}
export const UsersActions = {
async getCurrentUser () {
return async (dispatch, getState, { $http }) => {
await Promise.resolve({
type: 'SET_CURRENT',
payload: { name: 'Вася' }
})
}
}
}
import { UsersActions } from 'storage/users'
class HomeController {
constructor ($ngRedux) {
this.unsubscribe = $ngRedux.connect(this.mapState, UsersActions)(this)
}
$onInit () {
this.getCurrentUser()
}
$onDestroy () {
this.unsubscribe()
}
mapState (state) {
return {
currentUser: state.user.current
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question