Skip to content
Advertisement

How to pass in multiple action creators to single listenerMiddleware in Redux Toolkit?

I want to save state into my database whenever any of its properties changes. I currently have two middlewares that would dispatch my saveTrip function.

The two middlewares are identical but listen to different actionCreators.

Store.js:

JavaScript

Savetrip function:

JavaScript

I am not even using the payload thats passed in when I call saveTrip as I’m saving the entries state. I don’t have any bugs, but my code seems redundant if I need more listeners. Is there a better approach to this? I basically want to save my state into DB whenever the state changes.

Advertisement

Answer

You can use matchers

JavaScript

That said, you are using createAsyncThunk very wrong – your thunk will finish before the request is even made to the server, long before a response arrives. And you don’t need to dispatch something in the end, just return it. Thunks automatically dispatch a .fulfilled action.

Also, that debouncing will lead to very weird effects. You need to do that on another level, preferrably outside your dispatch.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement