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:

Radix Ui Adoption Guide Overview Examples And Alternatives

Radix UI adoption guide: Overview, examples, and alternatives

Radix UI is quickly rising in popularity and has become an excellent go-to solution for building modern design systems and websites.

Nefe Emadamerho-Atori
Apr 25, 2024 â‹… 11 min read
Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 â‹… 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 â‹… 6 min read
Exploring Zed, A Newly Open Source Code Editor Written In Rust

Exploring Zed, an open source code editor written in Rust

The Zed code editor sets itself apart with its lightning-fast performance and cutting-edge collaborative features.

Nefe Emadamerho-Atori
Apr 22, 2024 â‹… 7 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