Search code examples
reactjsreduxredux-promise-middleware

How can I pass through additional metadata with Redux Promise Middleware?


With Redux Promise Middleware, we write action like this:

export const withdraw=(id)=>{
    return{
        type: WITHDRAW,
        payload: (new PaypalContract()).withdraw(id),
        id
    }
}

Unfortunately, redux-promise-middleware won't pass 'id' in any of the "WITHDRAW_PENDING","WITHDRAW_FULFILLED" OR "WITHDRAW_REJECT". It only passes the promise in payload to action reducer.

In the real environment, we have lots of items to update, each item with an id to identify, so how can I know which item to update? We have to use 'id' to update specified item, but how with Redux Promise Middleware?


Solution

  • Redux promise middleware follows the "Flux Standard Action" (FSA) spec.

    You can use the meta property to include additional data in the action:

    export const withdraw = (id) => {
        return {
            type: WITHDRAW,
            payload: (new PaypalContract()).withdraw(id),
            meta: { id } 
        }
    }