Transcript Ā· Josef Bender
Data Sharing in TanStack Start: Easier than you think!
Speaker A
Tanstax Start is not normal. The ease at which you can create applications using Tanstax Start is not something you find anywhere. It's not something I've seen in any other framework. And I've been using tanstaxt since it was in beta. A lot of features have come along the way and every time I'm just surprised how easy it is and also fun to create Full Stack applications. In React. Before Tanstack Start, I would just call myself front end developer because I just didn't like dealing with other Full Stack frameworks. But with Tanstack Start, it's all beautiful. So in this video I want to show you one reason why you should definitely check out Tanstack Start. So I have this website here that I have built from scratch with Tanstack Start and this is Tanstack Weekly. You probably heard me talking about it either on Twitter or one of my previous videos.
This is a newsletter that talks about Tanstack Weekly. I send it every week. Check it out if you haven't already. But the point of this is that if you go to Previous issues here we have an archive of the previous issues that I have sent and if you go, let's say to the second issue, then you have the issue itself. And what I would like to have here is some kind of navigation buttons. So I want to navigate between the previous and the next issues. So it'd be cool to have a previous button here and a next button on the right. And the way that I found to solve this in Tazak Start is very beautiful because if you take a look at the application structure, if you go to issues, we are at issues and we are getting all this data from a third party tool which is in this case recent.
And if you click on a link you land in Issues id. Let's take a look at the application structure for that. So here we have our issues. So this is the index page and here we just show all the issues. And this is the list that we were talking about here. And inside Issues ID you have the component itself and we are showing it as a markdown. And this is what happens when you click on an issue. Now the problem that I have here is that the data that I am loading here is a bit different. So inside Issue id, let me just add a few spaces. Here you have this loader that gets an issue by id. So get issue function and this makes the API call to recent backend and then inside the index TS function file where all the issues are listed, we have this get issues function which returns an array of issues and this is what we are showing inside the list.
So what I want to do here is share this data that is in issues with the sub route id and for that we can take advantage of context. And no, I'm not talking about react context, which could also be a solution here, but it's just a lot of boilerplate code and also needs to work on the client side. And for this we're going to be using actually Tanstack router context. I'm saying router because Tanstack start is based on router. And what I'm showing you here also works in Tanstack router. So let's take this loader function and put it in a context and for that we're going to have a layout route. So let's create a layout. So new file I'm going to call this issues and this has to be the same name as the folder where the subroutes are. So issues tsx. This is of course generated directly because I'm running the development server in the background.
And we can actually get rid of this route component because this is not going to show anything, it's just going to show the children. And here we can load the data in the context. And for that we're going to be using a function called before load. So let me take this code from here, this loader, and I'm going to use it over here. And this is before load. And let me just import this as well. And before load is called before. Basically the loader is called. But what happens is that the result that we have here is also added to to the context. And now in the sub routes we can get access to this context. So inside issues we can replace this useloader data with use route context. You can see this data is right now there and it's also correctly typed. If I go back here and remove this loader function, save and go back, you're going to see that we have an error because actually Tazak Start has no idea what we're talking about.
But when you define a beforeload function, the type system just gets it automatically. And it's super easy for you as a developer, you don't have to worry about types matching and whatnot. Now, inside the ID route, I actually already have this previous ID and next ID defined with undefined. And I'm also passing them to this component here called newsletternavigation that is going to show the previous and next buttons. And now we just need to calculate the previous ID and the Next ID using a function in this case called getneighborentries. And this function is going to get the ID of the current issue that is right now open, so we can grab it over here. And this function literally just checks for the index of the current issue and gets the previous one and the next one using the index. And now that you have Previous ID and Next id, we can use it to show the navigation.
So. So let's go back to our application and there we have it. We have Previous and Next buttons. I'm showing them in the top as well as in the bottom. And when you click on previous, you navigate to the first issue, Next and Next again. And as you can see when we arrive at the end, the Next button is hidden because the value of Next ID is undefined. Thanks for watching the video. If you have enjoyed it, please let me know in the comments below. By the way, I'm almost at 10,000 subscribers, so. So it would be awesome if you could subscribe as well. That way we can reach 10,000 subscribers before the end of the year. Thanks again for watching and see you in the next video.