2022-02-04
2056
#redux
Andrea Chiarelli
256
Feb 4, 2022 â‹… 7 min read

Async actions in bare Redux with Thunk or custom middleware

Andrea Chiarelli Software developer and technical author. andreachiarelli.it

Recent posts:

Comparing Hattip Vs Express Js For Modern Application Development

Comparing Hattip vs. Express.js for modern app development

Explore what Hattip is, how it works, its benefits and key features, and the differences between Hattip and Express.js.

Antonello Zanini
May 2, 2024 â‹… 8 min read
Using React Shepherd To Build A Site Tour

Using React Shepherd to build a site tour

React Shepherd stands out as a site tour library due to its elegant UI and out-of-the-box, easy-to-use React Context implementation.

Onuorah Bonaventure
May 1, 2024 â‹… 14 min read
A Guide To Cookies In Next Js

A guide to cookies in Next.js

Cookies are crucial to web development. This article will explore how to handle cookies in your Next.js applications.

Georgey V B
Apr 30, 2024 â‹… 10 min read
Handling Dates In JavaScript With Tempo

Handling dates in JavaScript with Tempo

Use the Tempo library to format dates and times in JavaScript while accounting for time zones, daylight saying time, and date internationalization.

Amazing Enyichi Agu
Apr 30, 2024 â‹… 8 min read
View all posts

6 Replies to "Async actions in bare Redux with Thunk or custom middleware"

  1. Let’s say we have rest url to delete a resource

    / Users/Id

    How Do we dispatch action with id here as delete success won’t return any response according to REST principles.

  2. Andrea, how do we approach executing code after the async funtions have completed using redux middleware?
    Lets say we were saving a blog post using an httpAction:
    function saveQuoteAction(quote) {
    return httpAction({
    type: QUOTE,
    endpoint: “https://ron-swanson-quotes.herokuapp.com/v2/quotes”
    verb: “POST”,
    payload: quote,
    });
    }

    In my component I would import this action and then execute it:

    import { saveQuoteAction } from ‘…….’;
    const MyComponent = ({ history }) =>{

    const onSubmit = () => {
    ……
    saveQuoteAction(quote);
    // How do we wait for save quote to succeed if it is not a promise ???
    history.push(‘/’);

    }
    return ( ….
    };

  3. Hi Ralph_w, I’m afraid that there is a little misunderstanding.
    The saveQuoteAction() function doesn’t trigger the asynchronous action. It just creates a new meta action of type HTTP_ACTION.
    In order to trigger the asynchronous action, you need to dispatch it, like in the following:

    store.dispatch(saveQuoteAction());

    You should also have to implement a reducer that handles the actions related to the quote saving, similar to what you can see for getting a quote.

    In addition, you don’t wait for success or failure of the quote saving inside your component, but it is managed by the reducer. If your component correctly subscribed to the store, once the response arrives from the server, your reducer changes the state and your component will be automatically updated.

    Please, take a look at the example on CodePen (https://codepen.io/andychiare/pen/roXxpB)

  4. Hello sir i was following one of your colleague’s post

    https://blog.logrocket.com/data-fetching-in-redux-apps-a-100-correct-approach-4d26e21750fc/

    i replicated whole code in my project so that i can modify it according to needs and take it as an example

    but i am getting this error ,
    Error: Actions must be plain objects. Use custom middleware for async actions.

    and error occurs at very first line of his custom middle ware

    “` next(action); “`

    if i remove this line , there is no error and program fetches the article data but unable to set it in redux state.

    middle does not dispatches the action

  5. Solution :
    I had also faced the similar issue in the recent past. I did lot of research on it and found the solution on it. This is the very common problem with the people getting started.

    You must dispatch after async request ends.

    This program would work:

    export function bindAllComments(postAllId) {
    return function (dispatch){
    return API.fetchComments(postAllId).then(comments => {
    // dispatch
    dispatch( {
    type: BIND_COMMENTS,
    comments,
    postAllId
    })
    })
    }
    }

    OR

    Check if this works for you:

    https://kodlogs.com/34843/error-actions-must-be-plain-objects-use-custom-middleware-for-async-actions

    Hope this will help.

Leave a Reply