Friday, March 22, 2019

Error: Actions must be plain objects. Use custom middleware for async actions

If you got that error, it's likely that you forgot to import the redux-thunk and configure it accordingly similar to the code below:

1
2
3
4
5
6
7
8
9
10
11
12
import { createStore, Store } from 'redux';
 
import { reducersRoot } from './reducers-root';
 
import { IAllState } from './all-state';
 
export function configureStore(): Store<IAllState>
{
    const devTools: any = (window as any)['__REDUX_DEVTOOLS_EXTENSION__'];
 
    return createStore(reducersRoot(), devTools && devTools());
}


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { applyMiddleware, compose, createStore, Store } from 'redux';
 
import { reducersRoot } from './reducers-root';
 
import { IAllState } from './all-state';
 
import ReduxThunk from 'redux-thunk';
 
export function configureStore(): Store<IAllState>
{
    const middlewares = applyMiddleware(ReduxThunk);
 
    const composeEnhancers = (window as any)['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] || compose;
 
    const composed = composeEnhancers(middlewares);
 
    return createStore(reducersRoot(), composed);
}


No comments:

Post a Comment