Redux-Thunk "Actions must be plain objects. Use custom middleware for async actions."

redux-thunk vs saga
redux-thunk typescript
redux-thunk promise
redux-thunk mapdispatchtoprops
redux-thunk axios
redux-thunk async/await
redux-thunk - npm
redux thunk fulfilled

Creating my store with Thunk middleware

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
const store = createStore(
    reducer, 
    initialState,
    applyMiddleware(thunk)
);

And creating my action which calls a promise

export function getArticle(url) {
  return function (dispatch) {
    fetchArticle(url).then( 
      article => dispatch(setArticle(article)),
    );
  };
}

function fetchArticle(url) {

  return new Promise((resolve, reject) => {

    request
     .get(url)
     .end( (err, res) => {
       if (err || !res.ok) {
         reject('Oh no! error');
       } else {
         resolve(res.body);
       }
     });

  }) 
}

export function setArticle(article){
  return {
    type: constants.SET_ARTICLE,
    article
  }
}

In my article component I am calling dispatch on componentDidMount()

componentDidMount(){
  this.props.dispatch(
    getArticle('http://api.example.com/')
  );
}

But getting error: "Actions must be plain objects. Use custom middleware for async actions.".

What is wrong with this setup? I have tried calling compose(applyMiddleware(thunk)) but to no avail.


Your code looks ok except that it's missing how to handle errors (promise rejection). Your api might be returning errors and you're not handling it which could result to that error message.

Try adding

export function getArticle(url) {
  return function (dispatch) {
    fetchArticle(url)
      .then(article => dispatch(setArticle(article)))
      .catch(err => dispatch({ type: 'SOME_ERROR', err }));
  };
}

redux-thunk, Redux Thunk middleware allows you to write action creators that return a function instead of an action. The thunk can be used to delay the  Redux Thunk middleware allows you to write action creators that return a function instead of an action. The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met. The inner function receives the store methods dispatch and getState as parameters.


Change

return function (dispatch) {
    fetchArticle(url).then( 
      article => dispatch(setArticle(article)),
    );
  };

To

return function (dispatch) {
    return fetchArticle(url).then( 
      article => dispatch(setArticle(article)),
    );
  };

What the heck is a 'thunk'?, Well, this is exactly what redux-thunk does: it is a middleware that looks at every action that passes through the system, and if it's a function, it calls that function. Redux-Thunk is arguably the most primitive such middleware. It is certainly the first that most people learn, having been written by Dan Abramov as part of Redux proper before being split out into


Try the following:

export function getArticle(url) {
    return fetchArticle(url).then( 
      article => dispatch(setArticle(article)),
    );  
}

Async Actions, When an action creator returns a function, that function will get executed by the Redux Thunk middleware. This function doesn't need to be pure; it is thus allowed  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 body of the function once the asynchronous operations have completed.


In Redux every action must return an object.

You may use:

export const getArticle= url=> dispatch => {
 fetchArticle(url)
   .then(res =>
     dispatch({ type: SET_ARTICLE, payload: res.data })
   )
   .catch(err =>
     dispatch({
       type: GET_ERRORS,
       payload: err.response.data
     })
   );
};

Asynchronous Redux Actions Using 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  Now lets install redux, redux-thunk and react-redux. npm install redux redux-thunk react-redux. Now create a new folder under the src directory and call it actions. Next create a file under that folder and name it fetchAction.js. Once that is done create another folder under the src directory called reducers and create a file under it called


Thunks in Redux: The Basics - Fullstack Academy, If it's a function, it's given dispatch and the state. — Dan Abramov, responding to issue #1 in Redux. Redux-Thunk is arguably the most primitive  By default, Redux action creators don’t support asynchronous actions like fetching data, so here’s where we utilise Redux Thunk. Thunk allows you to write action creators that return a function


A Dummy's Guide to Redux and Thunk in React - Matt Stow, If you don't know what Thunk is, don't worry too much, but we'll use it to make asynchronous calls in the “Redux way”. This tutorial assumes you  One of those flux variants is Redux, created by Dan Abramov and others. If you never heard of Redux or don’t really know how it works, I suggest you watch these videos first. In this blogpost I’ll explain a way of dealing with async operations using Redux Thunk.


Redux Thunk Explained with Examples, Redux Thunk is middleware that allows you to return functions, rather than just actions, within Redux. This allows for delayed actions, including  Q: What is a ‘thunk’? A: The sound your head makes when you first hear about redux-thunk. Ok sorry, that was awful. But seriously: Redux Thunk is a really confusing thing when you first hear about it. I think it’s mostly because of that word “thunk.” So let’s clear that up first. thunk, n. A thunk is another word for a function. But