brianegan / redux_thunk

Redux Middleware for handling functions as actions
MIT License
88 stars 8 forks source link

Using a repository object #20

Closed hacker1024 closed 1 year ago

hacker1024 commented 3 years ago

In the Redux architecture sample, custom middleware is used.

The createStoreTodosMiddleware function takes a repository as an argument, and uses it to generate the saveTodos and loadTodos middleware functions.

With redux_thunk, thunks are usually declared as function literals or classes, and not created by a function, so this method can't be used.

How can a repository be used in thunks?

(An alternative would be to use dependency injection, but this seems messy and usually has runtime and/or build time performance impacts).

brianegan commented 3 years ago

I'll be honest here: I don't have a great answer for this question!

I normally use Epics or Plain Old Middleware myself over thunks for exactly this reason: I find it easier to construct my application dependencies in the main function as you see in the architecture sample.

If you really wanted to use thunks that have external dependencies, I think it gets uglier. As you mentioned, the only solution I can envision would be to build the Thunk Function with the dependencies it needs in the main function and pass it around manually. Or, use some kind of Inherited-widget (provider), build-time dependency injection (kiwi), or runtime service locator (get_it) to do the same job.

Like you, I don't like build-time slowdowns or run-time locators, so I just kind stick with the Good Old Main function.