Fix – actions must be plain objects. use custom middleware for async actions

If you get actions must be plain objects. use custom middleware for async actions error, here is how to fix this issue. This error exists…

The post Fix – actions must be plain objects. use custom middleware for async actions appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Pandu Rijal Pasa

If you get actions must be plain objects. use custom middleware for async actions error, here is how to fix this issue.

This error exists because you try to return a non-plain object on a redux action. For example, if you try to return another function, especially an async function, inside this action, redux doesn’t know how to handle this natively.

// redux action that returns async function
export const myAction = (payload) => async (dispatch) => {
  // your action
}

To fix that, you will need custom middleware injected on your redux. You can use redux-thunk or redux-saga. We will use redux-thunk in this article, so first, install the library:

// installing redux-thunk dependency
npm install --save redux-thunk

Then setup the middleware on your root redux configuration:

// importing libraries
import { createStore, applyMiddleware } from 'redux'; 
import thunk from 'redux-thunk'; 
import rootReducer from './reducers/index'; 

// creating the redux store
const store = createStore( 
    rootReducer, 
    applyMiddleware(thunk) 
);

The post Fix – actions must be plain objects. use custom middleware for async actions appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Pandu Rijal Pasa


Print Share Comment Cite Upload Translate Updates
APA

Pandu Rijal Pasa | Sciencx (2021-02-21T13:49:23+00:00) Fix – actions must be plain objects. use custom middleware for async actions. Retrieved from https://www.scien.cx/2021/02/21/fix-actions-must-be-plain-objects-use-custom-middleware-for-async-actions/

MLA
" » Fix – actions must be plain objects. use custom middleware for async actions." Pandu Rijal Pasa | Sciencx - Sunday February 21, 2021, https://www.scien.cx/2021/02/21/fix-actions-must-be-plain-objects-use-custom-middleware-for-async-actions/
HARVARD
Pandu Rijal Pasa | Sciencx Sunday February 21, 2021 » Fix – actions must be plain objects. use custom middleware for async actions., viewed ,<https://www.scien.cx/2021/02/21/fix-actions-must-be-plain-objects-use-custom-middleware-for-async-actions/>
VANCOUVER
Pandu Rijal Pasa | Sciencx - » Fix – actions must be plain objects. use custom middleware for async actions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/21/fix-actions-must-be-plain-objects-use-custom-middleware-for-async-actions/
CHICAGO
" » Fix – actions must be plain objects. use custom middleware for async actions." Pandu Rijal Pasa | Sciencx - Accessed . https://www.scien.cx/2021/02/21/fix-actions-must-be-plain-objects-use-custom-middleware-for-async-actions/
IEEE
" » Fix – actions must be plain objects. use custom middleware for async actions." Pandu Rijal Pasa | Sciencx [Online]. Available: https://www.scien.cx/2021/02/21/fix-actions-must-be-plain-objects-use-custom-middleware-for-async-actions/. [Accessed: ]
rf:citation
» Fix – actions must be plain objects. use custom middleware for async actions | Pandu Rijal Pasa | Sciencx | https://www.scien.cx/2021/02/21/fix-actions-must-be-plain-objects-use-custom-middleware-for-async-actions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.