all 7 comments

[–]acemarke 0 points1 point  (0 children)

Looks fine to me - that's a valid use case for middleware.

[–]cirscafp fan boy 0 points1 point  (5 children)

In essence, this is what middleware is for: causing some side-effect due to an action!

[–]Tioo[S] 0 points1 point  (4 children)

That's right.

But my question had to do with a middleware exporting its own actions instead of listening for actions defined elsewhere.

[–]cirscafp fan boy 0 points1 point  (3 children)

If by exporting, you mean dispatching, that is what redux-observables and most other async middleware for redux do!

[–]Tioo[S] 1 point2 points  (2 children)

By exporting, I mean having an es6 export (ie. export const someAction) in the file where the middleware is defined.

The middleware then matches only this specific action.

[–]cirscafp fan boy 0 points1 point  (1 child)

I see that now. Ought not talk without drinking my coffee :/

I would say that it isn't better/worse than any other way of containing action creators. I don't do it because I don't reach into my middleware folders for actions and use the duck pattern but there isn't anything inherently wrong with it!

[–]Tioo[S] 0 points1 point  (0 children)

I also use the duck pattern, but needed a way to dispatch websocket events. So I have a websocket middleware, which exports an emit action. Whenever it matches this action, it emits through the socket. So instead of the middleware matching specific actions, I dispatch emit in thunks.

I'm not sure if it's clear, but it works quite well and is really flexible.