So Remix allowed us to marry the backend and the UI in a way that has never been done before with the loader and action and our UI all in the same file. And this is a pretty simple demonstration of how that works from a route perspective. So we have our projects route and here we have our loader to load those projects and we've got our form to add new projects and the backend piece for that mutation.
But sometimes we have UIs that aren't like so URL centric, for example the Twitter like button, whenever I click on that like button that's not going to take me somewhere else, I don't only render that on a special page that has a specific route, I render the like button for every single one of the tweets that are on the page and so that doesn't really work very well as something that you'd like stick in a loader or an action for a particular route necessarily and here's another example of a combo box that's doing a search and this is the thing we're actually going to be demonstrating today.
So, I've got an implementation of this app right here, we're not rendering the combo box yet because we are going to build it together and connect it to the backend and it's going to be sweet. So, that's it. It's demo time. So, the first thing is we've got this app up and running right here, we're in the 01.b4 version of the app and we're in the app directory under routes and under this resources directory is where we'll find the customers. So, we're going to have this route for slash resources slash customers and that's going to be the API route that we're going to be using to go get a bunch of customers.
Now, one really cool thing about Remix is that if you don't have a default export of your module, the Remix will treat your module like a resource. And so, what we mean by that specifically is I can say export an async function, whoops, a function, come on, there, called loader and here I'm going to return JSON and that JSON actually is going to come from Remix run node and we'll say hello world. And if I save that and come over here, I can go to resources, customers and I'm going to get hello world. Now, there's nothing special about the resources directory other than the fact that my editor seems to like giving that a special icon, but there's nothing special about this. We can call this whatever we want and it just so happens that the way I like my URL for this to work is to have a slash resources and that's it. So wherever we want that URL to be, that's where the file is going to be. So by having a loader export but no default export, this is just a regular request for like an API request. So with that in place, now we can build some UI that interacts directly with this loader that makes fetch requests. But what's really cool about Remix is that we can actually add a bunch of other exports to this as well. We can export const koala equals Kodi, like it doesn't matter. We can do anything we want to in here and Remix will just ignore it in its builds. So what that means is we can actually export a component in here that consumes this loader and that's exactly what we're going to do. And because I know you probably don't care to watch me write a bunch of JSX, I've actually written all the JSX stuff there. So here's our loader, it's just what we had before, but then we got a bunch of other JSX stuff in here that you, like I said, you probably don't care to watch me do all of that. So skipping over the JSX bit, the most important and interesting bit here is we're using this use combo box, which is a Downshift hook. Downshift is a library I built years ago when I was at PayPal, and it's for making this combo box experience for us. And all we need to do is provide it with the items and we can respond to input value changes as the user is typing. So that's perfect. That's exactly the two things that we need. And with that we're exporting this combo box, but we're still just a resource route. There's nothing special going on here.
Comments