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:

Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Django’s server-side functionalities and React’s interactive UI.

Kayode Adeniyi
Apr 18, 2024 â‹… 7 min read
Using Aoi Js To Build A Bot For Discord

Using aoi.js to build a bot on Discord

Explore how the aoi.js library makes it easy to create Discord bots with useful functionalities for frontend applications.

Rahul Padalkar
Apr 17, 2024 â‹… 9 min read
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
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