Skip to content

How can I dispatch an action from inside the merge method on a query? #3463

Answered by EskiMojo14
LoyalPotato asked this question in Q&A
Discussion options

You must be logged in to vote

yeah, exactly

your slice's case reducers are exposed as slice.caseReducers, so you could do

extraReducers: (builder) => {
  builder.addMatcher(
    api.endpoints.getFoo.matchFulfilled, 
    (state, action) => {
      const { payload } = action;
      // remember that the case reducer needs to be called with the right action
      return slice.caseReducers.setListItem(state, slice.actions.setListItem({ item: payload, list: "bananas" }))
    }
  )
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@LoyalPotato
Comment options

@EskiMojo14
Comment options

Answer selected by LoyalPotato
@LoyalPotato
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants