As we transition into looking into how we use this function to load data into our route components, Remix is actually opinionated, and we'll talk about some of those differences. Next, we'll take a look at this index.jsx file in our routes folder. The Remix framework uses a file-based routing system, which is kind of similar to what you might get in Next.js or Nuxt if you're coming from the Vue world, so there is a decent amount of overlap in how this framework handles routing. And we'll look at some other more complex examples in just a little bit.
But so, if we open up our index.jsx file, we can see that we're importing some components in here, and then we're exporting this index component that returns all of these jsx elements that are going to render our application. But right now, we have all of our data hardcoded in this projects variable, and so the next step that we're going to want to tackle is loading our data dynamically from WordPress. So to achieve that, we'll just throw a couple more import statements up here, and I'm just going to copy those from offscreen real quick. We're going to import two different things. So we're going to import our get projects function from this WordPress service file, and then we're going to also import this use loader data hook from Remix itself. So once we've done that, then we'll come down here, and we're actually going to create another export, and so this is going to be an async function named loader, and that is a Remix convention. And what's going to happen is when Remix goes to render your components, it's going to call whatever is inside of this loader function on the server, and it's going to go out to your service, whatever that may be, in our case WordPress and GraphQL, to get some data, and then we will be able to access the data that this loader function returns using the use loader data hook. So for us, all we need to do in this example is add a return statement, and then we're going to await the result of get projects. So again, that's one of those reasons that I personally like to develop in this way. I like having a service file with all of my different data calls out there so that my route components can be very clean. Alternatively, you could certainly do some of this inside of this loader function. That's totally up to you and how you want to handle it, and to be honest with you, I'm not sure Remix really cares either way. But so we've got this loader function, we're doing some amount of work in there, and that's up to us to determine what it is that we want to do. And then what we'll do down here is instead of hard-coding this array we can come and we can just use the use loader data hook. So we'll set projects equal to the result of this use loader data function, and so if we save that out and refresh our page over here we can see that boom, that worked just as we expected. The load when this index route gets called, we go to render that component. This loader function calls get projects on the server, we return that data, and then use that data via this hook to populate these project components down here. So fantastic, we've got our index page working the way that we want, but I think it might be time to pause for just a second and discuss this opinionated choice that the Remix framework makes.
I've worked with a number of different Next.js projects and other React projects where some of the data loading that we're doing happens entirely on the client. And so if we're used to using the Apollo like use query hook, for example, and having those components make those calls out to our GraphQL server on the client, this is a bit of a paradigm shift, if you will, right, because each one of these loader functions is only getting called on the server. And I think the Remix team made that choice for a really good reason, and they've enumerated those reasons in a lot of videos, so I won't belabor them, and I encourage you to go check out some of the Remix singles to get a good idea of those. But in some ways, it does help us as developers simplify the type of code that we're writing, right? If we look at a framework like Next.js, for example, where maybe we're doing static site generation, and then some things are getting loaded on the client, it has a tendency to get a little bit messy, right, because we have all these different abstractions for these different use cases, where I think Remix sort of simplifies that. It says this is the one way that we're going to load data into components, and so it makes that a little bit easier. It also makes it a little bit easier to handle credentials, right? If I were making authenticated calls to a database, since all of these, this data fetching and the loader function is happening on the server, I don't have to worry about leaking my credentials into the client. So there are a couple of pluses to this that I definitely recommend you explore if you're new to Remix and kind of compare and contrast with whatever your framework of choice is at the time. So now that we've got our index page looking the way that we want, each one of these is actually a link to an individual project.
Comments