Avoid using global variables #1803
Unanswered
lukejagodzinski
asked this question in
Q&A
Replies: 1 comment 7 replies
-
If you are using Express you can pass a context object to the loaders and actions on every request from the Express server using the getLoadContext, see https://remix.run/docs/en/v1/other-api/adapter#createrequesthandler, this context is accesible in any loader or action. |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I have a question whether you have some pattern to avoid using global variables. Recently, I'm trying to avoid passing global variables around and unfortunately that's the advices way of doing it in Remix. For example, when you use Prisma, you supposed to import it from another file where it's already initialized. What I would like to do is something like this:
The
expressServerFactory
function would also define all the routes and passprismaClient
to each route handler factory function as a dependency. This way, I have zero global variables and code that is super easy to test, mock and replace modules without affecting the rest of the code.However, the routes convention used in Remix kinda makes it impossible. I've seen that you can define your routes in the config file but it's outside of the
src
directory so I can't use this pattern.Using global variables maybe is easy and tempting to use but it's programming antipattern.
Do you guys know how to apply this pattern in Remix?
Beta Was this translation helpful? Give feedback.
All reactions