Welcome once again to the React Show! Today we're gonna talk about how do you best structure a React application? And it comes down to one question. Do you build it out of one component or one component for every div? You were born a social animal in a world that was meant to be less measured, less exact. And now you sit with dancing photons blasting away your eyes as your muscles atrophy and your social skills lapse in a world where everything is measured and exact. You emerge from your cave that you call an office to find yourself torn away by the currents of the journey. Of the journey we are all bound together. Whether you're just learning to write code, or you're a 50-year expert that knows APL just as well as TypeScript, we're all on the same journey together. So join us on the React Show as we explore how to make better programs, better teams, better companies, and a better world together. The React Show is a weekly podcast brought to you from occupied Miwok and Bohatan Territory, focused on learning more about React, programming in general, and the intersection of programming and the rest of the world. Come join us on this journey. We're with Owl Creek, an organization focused on React and React Education, and we're super happy to have you along with us today. I'm your host, Thomas, with my co-host Austin. Hi Austin, once again, welcome to the show.
SPEAKER_01How'd you do?
SPEAKER_00Fantastic. Alright, so if you have any feedback or questions, like always, please feel free to email us at the React Show at Owlcreek.studio. And yeah, so uh like I said, we're gonna talk about, you know, do you make one component for your entire application, or do you make one component for every single div? Um in the React world, unfortunately, those are the only choices we get. Uh you know, it's it's that's the way it is. So, you know, on this episode, we're gonna take a look at, you know, making that, you know, trying to figure out which way to go. Um Right.
SPEAKER_01I mean, you don't you don't think about it, but like if you read the React tutorial, they do like the hello world, and there's like React DOM render, and then you have an H1 tag in there, and it's like it's implied that you could write all of your your app differently from there, but everyone like sticks their whole app in one component, right? And then they begin to branch down from there. So it's like it's interesting to go back to that paradigm of how how do we really start all the way at the beginning.
SPEAKER_00I actually am quite lost on that. I was really just making a ridiculous joke, but it sounds interesting.
SPEAKER_01Oh, me too.
SPEAKER_00Okay, I love it. Uh I love it when the jokes go right over my head because I make jokes and that never happens because they're never that great.
SPEAKER_01But like the one about the airplane?
SPEAKER_00I don't know that one.
SPEAKER_01The airplane that went over your head, too.
SPEAKER_00Uh oh, okay.
SPEAKER_01That's like a dad joke, I think.
SPEAKER_00All right. So now that we got that out of our system, apparently. Um so we're gonna we're looking at how do you um, you know, build a piece of React software that can be scaled and maintained long term. And so the way the approach that we take is we we look at the um architecture first. We look at the the user's problem, um, and we try to figure out okay, how do we take that and break it down into the right abstractions? And to sort of help make this less theoretical and more concrete, we're gonna take a look at a very specific use case, which is an e-commerce storefront for dog clothes, you know, specifically for French Bulldogs. So we're making a, it's our new, you know, our new thing, we're making an e-commerce storefront for this. Um, where do we start? We're gonna make it in React. What do we do? Um, so I'm curious, uh Austin, if if you were given this situation, which you are, you know, I know you're building this storefront. So so what is what's going through your mind? What are you trying to figure out to start with?
SPEAKER_01Well, this is where I'm like, okay, I go to the go to the user journey because I'm trying to build an app where like ultimately the only thing that matters in an e-commerce store is the flow of the user through your app to the checkout as quickly as possible. So my goals would be like, okay, how do I create something that when they have found my ad or my post or my whatever click into the funnel that when they land, they get something that's like so dopamine inducing, and then it they just immediately there's like a super quick, happy path to checkout and getting money into my Stripe account. Like that's ultimately my problem I'm trying to solve. So uh breaking it down, I guess, would be like, okay, I have this concept of a landing page, I have some sort of checkout system that I need to do, and I have some sort of like being able to actually do the payment and then being able to deliver uh a receivable to that customer. That that's kind of like the breakdown of an e-commerce store in general, yeah, right?
SPEAKER_00Absolutely. So the things running through my mind now after you described this user scenario are you know, questions like do I need to have deep linking into product landing pages? Do I need to allow the user to go back and forth in the checkout flow? Is there maybe checkout software that already exists that we could utilize? Should we be utilizing it? Um Are we really concerned about um SEO and things without JavaScript running being able to access our page? Are we um interested in you know um supporting all different screen sizes? Like these are all the questions I'm asking. And then I'm also like thinking, all right, so if I start breaking this down, what is the data that underlies this? So, you know, we have data for maybe our products. Um, do we need to track how much is in stock? Do we need to track prices? You know, do we need to have limits? Do we need to have discounts? Um I don't know if you have answers for any of these yet, Austin, but these are the things also that I'm thinking of when I'm trying to even figure out, you know, what choices to make at the start?
SPEAKER_01Well, luckily uh we do audio transcriptions on our podcast internally, so I'm gonna make sure I have a list. Uh uh I'll get all those questions and I'll have answers to you uh maybe afterwards when we sit down and make this econ store. Yeah. But no, I I like uh the the depth of questioning up front. Uh like before getting started, even architecting it, you have to ask all of these questions so that maybe maybe you don't like begin building your software to account for these things immediately and like pre-abstracting all this insane stuff, but you have these things in your peripheral for how you're moving forward so that when you create the MVP or the whatever you're creating to get it off the ground, that you can begin to extend in those directions without tripping over your own shoes.
SPEAKER_00Yeah, yeah, that's exactly how I would have said it, actually. It's it's perfect. Um, we're not looking at answering these necessarily, but like, you know, at least being aware of it and especially being aware of any questions that have a big influence on the choices we make. Um, you know, so like if if we were like, oh, a really important part of our strategy is to have search engines crawl our store and give us search engine traffic, you know. Um if that's really important to us, then we need to take that into consideration right up front when we start picking out the technologies we're gonna use. Like if we just use Create React app, it doesn't do that. We're not gonna solve our problem. But you know, there are pieces of software that that would do that. Did do you want to talk about that a little bit, Austin?
SPEAKER_01Yeah, so like as as we begin to build like our own tech stack internally and our own suite of expertise and having done React for uh since it came out now and having kind of experienced a lot of the spectrum, uh, two of the libraries that I like when when I think about making a a store or any website with routing, is I'm like, I definitely want to be able to do it with Next.js uh because I I like spending the time building my own stuff and like figuring out what APIs I can scaffold out for future projects of the same type. Because it comes back to our last episode, like trying to build a fundamental tool suite to assist like the development team of any endeavor you develop, and like maybe there's a little startup cost, like instead of just using a Shopify store or something for your thing, but like if you if you find a good tech stack that you can work with and iterate on, you can really move rapidly and then have like that toolkit for future anything. So like Next.js is really great for routing in general, like you don't have to install any of these libraries for it, you just make your uh pages per per route under the pages directory, and then you also have really easy access to a uh quite simple uh like serverless API uh architecture folder. So that's like really great to be able to say, okay, I can make a server if I need, I can make all my API routes here, I can have all my pages here. Um, and then the next one we've also talked about is Shocker UI. Uh Thomas and I have been through the Ringer with each and every CSS library and all these different things uh as as uh the ecosystem has evolved. And we really have enjoyed working together using Shocker UI to get really explicit about creating our own component libraries. Uh the verbosity of Shocker UI is really mitigated once you start to learn how to create effective reusable components for yourself and your projects, which can be really easily themed. And then uh having like this shared like Shocker UI in itself has implemented this tooling into their own saw uh library to like you can use their paradigms across all of their components really well. So like the these are uh these are really good examples of libraries that we we rely on and we've chosen them because they internally live by like the tooling processes that we desire and they help us to develop faster too. But like once we've these are just some examples of requirements that I would use on this project, but um beyond that, like thinking about some of the the other stuff that is agnostic to uh any libraries that's just kind of plain React, uh, I think that's where we would go into next, perhaps.
SPEAKER_00Yeah, although unfortunately I'm really getting hung up on this uh serverless thing, and I just want to you know make some snide remark about how all serverless servers are backed by a server.
SPEAKER_01Um yeah, I know.
SPEAKER_00And and uh I feel like we serverless in the Yeah, I feel like we posed the question of you know, what would you use if you needed something that had good SEO? And we gave an answer, but we forgot to mention that it is the answer as well for that, which is Next.js does handle that situation of um providing if you use it correctly, it'll provide uh data to the search engine. So it's a great choice there. Um but yeah, Austin was totally right. What do we do sort of regardless of what technology choices we make? Um, what are we gonna do in terms of structuring our um project itself? Like, you know, are we looking at uh really sort of specific things? Like, are there specific lines you should draw per se? Like if if you had um different folders, say a folder for hooks, a folder for components, you know, context, like is there uh a better way to do that? Um I think it's a you know, maybe it's not a super important question, but at the same time, I know from talking to people, they especially people new to React, it's one everybody wonders like, is there a best way to do this? Um and I think in you know, in our show notes here, we're missing one thing that I often see that I actually don't like. So I think that's cool. And that would be um you have like a components folder, but you also have a containers folder. Um, and I'm so I'm not actually not a fan of that. To me, if I'm looking at this, I want all my components in a components folder because then I can find them all. Um, I'm not sure. Do you agree with that, Austin?
SPEAKER_01Um, well, I mean, the there's like this kind of abstraction that I've been trying to do as I've been creating components with Chakra UI is trying to make like really uh quote dumb visual components where I can how much uh visual stuff can I do that I'm just passing props to it, and this component doesn't have to, there's no context outside of styling uh HTML CSS and just visualization within these components, and then trying to figure out the proper way to abstract out logic into like some people are choosing to do containers or views or something. But I do think there needs to be some sort of clarity in in dividing these two separations of concerns because it allows like you to perhaps test your visual components much more easily versus you look into it and it has all this complexity where you're like, oh man, like I can't even like test this to make sure it's doing what I want it to do, right? Or like the the logic that should be up there is down here. So I think I think that's a good topic. I'm not sure how to do it best. I'm still tinkering with the idea.
SPEAKER_00Yeah, I mean it's really interesting to me your your answer there. I I'm I'm not in the same boat, actually. I think uh I I've been through a lot of different sort of paradigms, you know, over the years in terms of this like MVC and uh I can't remember all the different acronyms or whatever anymore. Um I what I do is I start with a single large component, um, and I just write all my code in there. And what I abstract out are uh well, I guess I don't know, like it's it's all about building the right abstractions in general to me. And so part of what I do is I find um similar um sort of generic things, and I'll abstract those out. So like um in my projects, I I have like a shared library that I use across all my projects, and that has been built up by extracting things from the code I write. And so I've got like you know a form field that is connected to React Hook form and all sorts of other, you know, things for forms where I can quickly build forms, or I have like um a sort of my own component library based on Chakra UI that allows me to quickly build user interfaces in a way that will look good on a lot of different screen sizes without putting a lot of effort into it. So it's those kinds of things that I look for first. So if I'm like, okay, I have a button and I have this pattern around my button, I'm gonna abstract that into a new component. Um I personally don't find a lot of value in creating like view components or anything like that. I know I'm probably in the minority there. So, you know, your what you do with this information is is up to you. There's a couple different routes you can go, and I think probably trying them both and seeing what you like best, it would be my recommendation.
SPEAKER_01Well, I do think you make a good point though, because if you uh like your approach is definitely more of the quote bottom-up, and if you focus on really abstracting out all of these shared like buttons or whatever, your import, then you have like maybe a lot of imports of all these little visual uh reusables, but then your your like page or your view you're building is actually quite minimal to some degree in its verbosity because you've you've abstracted out uh at a at a better level that is uh much more recomposable throughout your your entire product uh or across multiple ones, like you say.
SPEAKER_00Yeah, I I think that probably is what it is, is the the bottom-up approach. But I'm also like part of it too is I I like um I have in fact I like large files. Um I often will put multiple components in one file. I I see no benefit to files or very little benefit to a hierarchical file structure. To me, I operate on my code based on you know features within my um editor that allow me to navigate directly between references, or I just search and I put tags in my code usually so I can quickly navigate between different areas and I don't have to open up a new file, remember what the file name is, remember where it was in the hierarchy. So, like, you know, in projects that I do my own, you'll probably notice I don't have very many. And actually, this is a criticism I have of Next.js is I feel like Next.js kind of encourages you to have more files and more folders. Like each page is a file within this other folder. I I find that super annoying. I want to put all my pages in one file. Um but yeah, maybe this is just me ranting on my my things nobody really cares about.
SPEAKER_01No, I do, I do like, I think it it comes down to you have a different proficiency than people like it's about learning the like how to navigate your files properly up front is the real problem versus like how to even structure your project, right? Like uh having a large, large file shouldn't really matter. It's just like how do you navigate it if you're able to, it doesn't matter if it's big or small. Like you can warp speed to I mean, what's the difference between a reference to a function written in one file to having it abstracted to another file other than like the visual reference you have to finding it, right? As long as it's in like its area with its context, it it really shouldn't matter where it is, right?
SPEAKER_00Yeah, I you know the this is really interesting. Uh I think what we're really pulling out of this is the the real answer here is you know, maybe you can look at other projects and see what they do and copy them, but the the real answer is to try it out for yourself with the eye that you're experimenting and trying to learn and and get better, and try different techniques and find out what works best for you. I don't know, that's what I'm hearing at least.
SPEAKER_01Well, yeah, and the techniques of structuring it go beyond just like how you structure it, but it's like how you work with your development environment, right? Because the goal is to like have an efficient model for your application, and that like just because you're driving, like we talked about in our last episodes, like one of us is driving a bike, uh riding a bike, one of us is driving a car, it's like, well, look at the vehicle you're in. Like, uh, do you are like, should you do something to the vehicle instead of just driving it around? Um, but like kind of back to the we talked about this e-commerce like uh platform idea. So like s structuring out a project with that. I've chosen to use Next.js and Shocker UI for like a landing page, just like creating a single page that has like uh a button that or why even make a button that goes somewhere, right? Why not just like Like you said, make a large page where on the home page you have the entire storefront like filterable or searchable or navigable with some interface with like a check checkout button that floats around and follows you, saying, like, hey, this is the only call to action we want you to take is the buy button, right? And it's like it's waiting for you to make some selections to hit go. Yeah, that that would simplify your need to create multiple pages, have different things, it would all just be in one place, and the user wouldn't have to be jumping around to make choices, right? They would only have one choice, which is to check out, right?
SPEAKER_00Yeah, I think that depends again on your goals. If we were talking about you know being able to deep link a product, like say you want to post a specific product on your social media account and have people click through to just that product, um, you might also need pages for each product, or at least have the home page. I guess another approach is it could be like an uh an identifier to the link on the homepage that filters it to just that product. I mean, there's there's multiple ways of looking at it, I suppose.
SPEAKER_01Right. And uh like that could you could you could have the like using query params so you don't need multiple pages. Um I think we kind of have uh a problem with one of our projects where our query params don't work. I think we need to look in that may so this is where you can cross-leverage uh projects you're working on to create tools to fix other projects, right? So I think we have the deep linking solution coming up here.
SPEAKER_00Yeah, yeah, sounds good. Um yeah, so I think that that's interesting. I'm curious um your thoughts on like actually structuring components themselves, and you've kind of already heard my thoughts. I do not ever create a folder for each component that has an index file in one file and then a component, and then I I usually just have my components folder, and that is just filled with um component name.jsx, you know?
SPEAKER_01Um yeah, you know, um I actually totally agree with you, and the only reason so this is what's like this is what you have to like really cultivate your own opinions and make sure that like you don't just implement the opinions of others just because they like tell you to. Um I I remember I was working on a previous uh a project we're working on now, and one of our teammates was like complaining that one of my files was like huge. And I was like, Well, dude, none of these components are related to any other component than this view, so they should just be here easy to reference. And he like kept complaining about it, and I like I broke everything down into separate files so he would shut up. And now I I'm like in some way, it's like I've taken on this paradigm and I don't even want to to live by it, you know? Yeah, uh it's very it's very dangerous uh like not cultivating your own opinion. I think that's like the main point of this talk is to like really figure out like why you're doing what you're doing and just like don't worry about what everyone else is saying, just analyze your use case and make something that works for you.
SPEAKER_00Yeah, and I think really, Austin, what you're bringing up is another case of the situation where you might be looking at the best abstraction or you know, building things in a bottom-up approach versus a notion that people have built up. Uh a lot of programmers have notions about things where they think something is inherently worse or or something. So, like there's a thing that goes around where a lot of React programmers will think a large component is inherently bad. And they will just always break down components that are big into smaller components. To me, that's absolutely the wrong way to look at it. You're supposed to look at it in terms of finding the right abstraction. File size is not an abstraction we care about in our program itself. You know, so like to me when I hear people say things like that, it's a it's sort of like, I don't know, they're creating busy work for themselves and maybe it makes them feel good, but like it ultimately doesn't make your program any better, right?
SPEAKER_01Well, I mean, yeah, the all I did to like break all that stuff down is I had something I was comfortable working with. Like I don't think anyone else, like that was my area of concern in the project. Um, the the teams are changing anyways now, things are different. It didn't matter if I rewrote it or not, but I've now already rewritten it. I spent like three hours of my time rewriting it ultimately for something that I would just kind of gladly revert, you know.
SPEAKER_00Yeah, I I think this is I think it's cool we we you brought this up and that we're talking about this in this episode because it's something I didn't think of to bring up, but it's really important. And Austin, you actually did touch on this earlier, which is just like um you will get a lot of opinions and viewpoints from blogs and from people in the industry and coworkers that tell you how things should be done. Um, and when it comes to stuff like this, I find basically no value in that. I mean, maybe you can tell me, hey, I noticed that if I do it this way, I get like this benefit. That's one thing. But if you're getting or seeing things where people are just like saying, Oh, you should always structure your program this way, you're you know, make your components in their own folder. You should always have container folders with container components, and you should always, you know, do don't look I I don't know if I should say don't listen at all, but don't pay much attention to it because it doesn't have much value. I mean, ultimately, it's up to you to look at the program you're building and the the best way to make it have the highest quality and blindly following, you know, industry best practices is not gonna lead to a good program. It's just it's gonna lead to a mismash of other people's ideas that you probably don't even fully understand.
SPEAKER_01Well, this brings me to uh like when I ended up choosing Next.js as my go-to. It was like someone was like, Hey, have you heard of Gatsby? And I'm like, Oh, it's Gatsby, and it's like, check it out. There's like this starter package, and like blah, blah, blah. And so I like I ran one of the installations of like uh the blog example, and the the paradigm of writing a website in that framework was just so specific to it, and people were like, do this, do this, it's good. I'm like, Well, I don't want to have to like no, this is hurting my brain. And then, like, I I so many people are like, hey, which which programming language should I start with? And it's like, how many people get stuck on that question for like years, maybe, right? Before they even decide to write a hello world in in like Python or something, you know? Just like pick something, like follow your heart, what does it say to try to do? Make messy mistakes, learn from what other people are doing, but don't prescribe to anything. Like you're capable of making smart choices, and that doesn't mean yeah, totally discount everything, but you're always supposed to be learning about like what choices to make going forward. And so that's why we're here and we're like really big on education in general, and not preaching away, but just sharing our experience of what we think has worked for us and trying to create a conversation about what can work for everybody or work for you or what whatever works, right?
SPEAKER_00Yeah, I think it it's uh you know, this is something I've run into a lot is people, especially beginners, are looking for me or people like us to tell them the answer to say, hey, put your components in folders like this or whatever. That's what a lot of people seem to be looking for. Um, but I'm never gonna do that because I don't think there is always a right answer. And I think part of the journey is doing and and understanding it yourself. Um so yeah, I unfortunately we we gotta start wrapping uh this episode up. Um, you know, I'm sort of get starting to get a little fired up on this, but maybe we'll have to to save it for another show. Um did you did you have anything else you wanted to mention uh before uh we wind things down here, Austin?
SPEAKER_01Well, I actually love how this episode turned out. Like I I was like we talked about the podcast planning of what we were gonna do, and I like I looked up some like React folder structure stuff, and I was like, man, this like this is gonna be boring. And then like we we just started talking. We had the first the episode before this, and it like changed the whole rhythm of how this goes, and it's like more of like a thought mind-opening uh approach of like how to write software in general. So it's kind of like a clickbait title, but I like how it worked out a little, you know.
SPEAKER_00Yeah, I I totally agree. Um when we were you know going over this episode before we started doing it, you know, the planning it out. I was thinking like the same thing. And yeah, it really I I like how it turned out, and I think really it it sort of showcases our views on on how to learn, and and that's really important to us, is it's it's more about you and and and finding what works for you, like Austin said. You know, there is no best prescribed way to do anything when it comes to stuff like this. So I I think it was really great that that this is what that uh what this episode has turned into.
SPEAKER_01Well, I mean, it we still I feel like there's some really decent, good value in terms of theory and practice. Like I had this concept of this e-commerce store. I I want to sell some stuff to some people, and like it doesn't have to be any more complicated than that. People try to like turn it into like some big huge discovery. It's like, hey, what are you trying to do? It's like I'm trying to do this. Okay, break it down. What are like what are you trying to do? Just like spitfire a bunch of questions, like, hey, like what have you thought about this? What about this edge case? Like, what are you thinking about this? And just it's like, oh, okay, mm-mm, mm-hmm, okay. These are giving me ideas of like the potential and the things I need to be aware of. And then it's like, okay, I have this really simple goal. How do I start and just make something that works really quick? Make it like don't worry about the way it's structured, just make the thing, and then make it in such a way that you can just iterate from anywhere in in any direction. Leverage tools that help you do what you want. Don't bring on things that are going to slow you down from the beginning for no reason, and just like focus on creating your idea and don't get caught up worrying about the right way to do anything.
SPEAKER_00Yeah, I couldn't couldn't have said it better myself, Austin. Um, thank you once again uh for for being on the show with me, Austin. This has been a lot of fun, and I want to thank all of you listening for joining us. Um we're gonna wrap up the show here, and uh we we look forward to having you with us again next week. We can't wait. Um yeah, so uh thank you once again and uh take care. Have a good one. Ciao.