How to Remix Your Stack with ReactJS
Hey, let's talk about how we can remix our stack in a Monorepo workspace. And I would like to start with a quote, or actually a tweet from Ryan Florens from a couple of weeks ago, where he actually made a quite powerful statement and mentions that your routing files should actually just configure the routing for your specific stack. The actual implementation of that feature that's being visualized there, should live somewhere else, right? So, you import it, but then specifically depending on the stack you have, you configure the routing and you import it, and then it gets bundled and visualized on the webpage.
And this is a very powerful mechanism for architecting your system, but also for organizing your code. And we can take this even further. So normally, we see exactly this type of structure, what Ryan Florence mentioned. So you have kind of your application, you have the routing folder, which can be pages, routes, app, whatever. And then you should have, as Ryan mentions, like a different folder potentially inside that app, which is like your features. And then you have like the different folders for all those features. And the routing simply imports from them.
Now we could actually go a step further and even move them out of the application into dedicated packages folder. So this new structure, what you see now is we have an apps folder and now we have a packages folder. And in the apps folder, we still have our application with its routing mechanism. And then in the packages folder, we now have different folders for all of our features. And all of these features are now nicely kind of independent encapsulated, and they have a very clean API point where there is an index.ts file which exposes everything that can be used outside and everything else kind of remains inside that package. So you can imagine we get a very clean structure having such a setup here. Obviously, we can go ahead and just import as before. Our import might even look like as if we consumed it from some external package like some MPM registry. But it actually is just consumed locally in this case. But you can see it here from such an import statement.
So how does this help, actually? Well, potentially with something like incremental migration. Now, you might have noticed that in my example, I used a Next.js app, right? There's that pages folder. Well, if you use the new features or opt into new features, you might have a different routing folder there. But this was not by accident, but it was intentional. So you might nowadays have like a Next.js application with some already built in features, with some like an application that has been developed already for a couple of months or even years. And you might want to kind of slowly move over to a remix stack because for whatever reason, that is more suitable for you. So this is exactly what you could achieve in such a setup, because as you can see, our actual Next.js app and the actual feature folders are kind of already separate and nicely. And if you pay attention, this is actually already a monorepo. If you want, right. We have just a single app in there, but we have an app.
Comments