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:

Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 â‹… 11 min read
Using Aws Lambda And Aws Cloudfront To Optimize Image Handling

Using AWS Lambda and CloudFront to optimize image handling

Leverage services like AWS Lambda, CloudFront, and S3 to handle images more effectively, optimizing performance and providing a better UX.

Nitish Sharma
Apr 12, 2024 â‹… 12 min read
Building Web-Based Terminal Components With Termino.js

Building web-based terminal components with Termino.js

Explore Termino.js, an open source library for integrating web-based terminals into applications, in this introduction article.

Chibuike Nwachukwu
Apr 11, 2024 â‹… 6 min read
How To Build A Custom Gpt: Step By Step Tutorial

How to build a custom GPT: Step-by-step tutorial

Let’s see why and how to build custom GPTs — personalized versions of ChatGPT that act as custom chatbots to serve a specific purpose.

Peter Aideloje
Apr 10, 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