SPEAKER_00

And welcome to the React Show. Brought to you from Occupied Miwok Territory by me, your host Thomas. And some spice, episode 71. Use effect. Being used wrong, like it often is, and testing React apps are the two worst parts about React. Today we'll take a look at my experience porting a React native app to web. The challenges with that and what you can do to do things right in your own apps. Thank you for joining us. I always find it fun starting a new project, especially one where the criteria is pretty well defined, so you can just get into a rhythm and start cranking out code. I also find that it like highlights pain points in the systems, you know, that you're using. Like when you've been working on a project for a while, you sort of get used to the way things work. Um but when jumping on something new, you haven't been like conditioned to it yet, and the painful things really stick out. At least I mean that's how it seems to me. Um have you ever noticed this on starting a new project as well? Uh I've noticed uh that our favorite youper, Jay, is here to join us today again for a show. Welcome back to the show, Jay. Oh, hey there, you know. Thanks for having me again, eh? It's uh always a pleasure uh pushing you to tell the people just how much you actually dislike React. Hey, that's not fair. Who says I really dislike React? I mean, you know, you you always go on and on about how great Lisp is and how your custom-made scheme is the B's knees. You can't fool us. Okay, okay, yeah. I do like Lisp, and uh, you know, I wouldn't complain if I could write my React apps in Lisp, but I actually do like React, even if it isn't Lisp and isn't perfect. Oh yeah, is that so? Uh I heard you've been porting a React app from React Native to web, and there were some things you really didn't like about that. Can you tell me more about that project? For sure. Yeah, so I'm currently working on a project where we're porting this e-commerce React Native iOS app um you know that also has some like crypto features uh to the web. Um it's been a really interesting project, and you know, there are always complications, and this port is no exception. Uh, we chose to build the web version on NextJS, and NextJS, and well, to be honest, I think the future of React in general really differs from the way that older React code was written, and especially the way React native code is generally written. You're telling me that things change in the React ecosystem? That can't be. I thought things never changed in the JavaScript world. I mean, that's why we all liked IE6, you know? Oh my goodness, IE6? Yeah, that's right. We all loved that IE6 held us back for an entire decade. I mean, what are you on, Jay? Um yeah, but yeah, things things have changed. Um if if you listen to our uh December news episode, you'll know that React is moving away, I think, from doing everything client side. And really Next.js and Remix have already been pushing us in this direction. Um, but this React native app wasn't written using those. Of course, it was written in a more old school style where everything is done client-side. And well, being a mobile app, this actually kind of makes more sense, anyways, than the way that like Next.js works with pages and its own like API infrastructure. But at the same time, there are better and worse ways of doing things client-side. And I think this React Native app really took uh maybe the worse approach, uh, especially for data fetching. And this isn't you know a dig at the team working on that app. I think it's actually very normal and the way that I see most uh React programs being written. Um so yeah, I think it's not abnormal, uh, but I definitely, you know, when when writing this new, you know, doing this port, writing this new web version, it seemed pretty clear there were some serious deficiencies in that approach. So just like uh how everyone except Firefox abandoned their browser engine and and just uses Chrome underneath, eh? I mean, I don't quite get the connection, but yeah, well, I wouldn't say just like that. But in this React Native app, they they do nearly all of the data fetching in what I would call the wrong place. You know, it's really super common where you are like, hey, this component needs this data, so I'm going to, you know, stuff all the data fetching for this component into this component. Um, this leads to a bunch of hooks, in this case, React query-based, and like use effects all over fetching and syncing the data, you know, with the app. Um, but it leads to a real mess because the data isn't necessarily going to be available when the component initially loads. So there's all kinds of casing and special code to show loading states and handle partially loaded components and you know, in one sense, this this isn't terrible since if you do it right, you can kind of have this sort of progressively loaded app where things fill in as the data arrives to the client. Um, but if each component is fetching data this way, it creates a real mess trying to synchronize the data within the component and the app. You'll often see this when you see a bunch of like use effects inside a component that act based on the data that's getting loaded in. This leads to just generally more complex and bug-prone code because now you're dealing with side effects and behavior that is determined based on the time and network events in like this very loose fashion. Oh yeah, you know, I've seen that a lot where so much code and and and logic, you know, just ends up getting crammed into to use effects. Uh is there a way around this though? Like, I mean, what else would you do? Oh yeah, definitely. Um, there's definitely a way around this. Uh, in fact, I think a pretty natural way. So, one approach you can take is to create your data subscriptions in your events. So, like, let's say you have a button that when the user clicks it, it opens, you know, a new screen in your app or a new page or whatever. And that new screen or new page is a you know a new component that displays a bunch of data. The naive way to implement this is to fetch and set up all the data inside that component on the new page or the new screen. This feels and and initially seems like the right place in one sense, because you're like, hey, the data is for this component, so I should load it within this component. But it actually leads to the use effect hell, as I call it, um, for the reasons I mentioned earlier. A much more correct way to do this would be to set up a subscription to the data inside the actual button event handler that the user clicks before the component for the new screen is even loaded. Um and this uh even leads to better performance because you start fetching the data sooner in the process. Um and it eliminates issues that a lot of people have had, you know, in moving to React 18 where they're like fetching data inside their useFX and you know not really doing it right and in React 18 and in strict mode, it's always in in development mode, it always loads your component, unloads it, and then reloads it again as a way to sort of point out to people that they were doing things wrong and that they shouldn't depend on you know use FX only running once. Um and so yeah, if you program in this what I call the more correct way, where you start your data fetching based on user events instead of based on like component life cycles, um, you'll end up in a much better place. Um and this is actually the place where I made some big changes in the porting process where I deviated from the original uh architecture of the React Native app. Oh, I mean that makes sense, but uh like specifically, what did you actually do different? Like, did you set up the data subscriptures in the event handlers then, like you said? Uh I mean actually no. Um, so I just read it on all talking about that. But actually, I think with Next.js, a much better place in this um app for the data fetching was actually in the get server side props function on a page. Uh in fact, so far, I only have written or used one use effect, and that was for a special case in a provider for some client side only specific code. Um so yeah, if you're not familiar with it, get server side props is actually code that gets run server-side on a node server. And it is supposed to return data that for your page component, actually that your your like page component actually needs to render that page. Um and Next.js actually, you know, sort of handles that for you. It, you know, your component isn't gonna render until it gets the data that it needs. Um, so I moved nearly all of the data fetching into get server-side props. This allows the actual React components to be much cleaner, as they don't need to do any data fetching or deal with like time or networking events or stale data. They can rely on exactly the data they need being available on the very first render. Um, and the net effect of this change in architecture is I think a lot less code overall, and you know, more importantly to me, a lot less branching and just possibilities for bugs to creep in. It makes the React components much simpler. Um, and and now I I know that's not actually a solution for everything, um, but I have found that it really is probably the best solution for most data fetching. And in fact, I believe this is actually the way the React ecosystem is headed in general. Ah, interesting. What do you what do you mean by that though? Yeah, so with the new uh server-side components and really a lot of what was laid out in React 18 and the changes that NextJS and Remix are making, I think we're going to really see data fetching move into server-side components. I believe actually, even in the beta docs for the new Next.js app page architecture, they even say that all data fetching should be taking place within server-side components. Now, technically, get server-side props is not a server-side component per se. That existed before you know React 18 and server-side components. But I think the idea is generally the same. Basically, we can provide a much more progressive and better user experience if data fetching is done server-side, and components can be rendered using that data directly on the server. So Gas Server-Side Props allows for developing a better user experience in the in that sort of sense, even without the new React 18 features. And I think you know, writing it in this style will make it much easier to migrate code to you know whatever these new architectures come out that are sort of based on React 18 and server-side rendering. Um, but ultimately, you know, I think both of these approaches will make much cleaner components that don't need to rely so heavily on, you know, use effect and and things like that. Oh, yeah, that actually makes a lot of sense, you know. Um, I'm definitely gonna give that a try. I I feel like um, you know, use effect it at first I used it everywhere and it seemed great, but I don't know. It definitely seems to sort of lead to a lot of really complicated and like hard to understand, you know, code, right? Um, yeah, so anyways, I'm just wondering how do you feel about use effect in general then? Oh boy. Um, I have so many thoughts. Um, but mostly I think people misunderstand it and and just use it wrong. Um, so really maybe the blame should be placed on React and the React team then for not doing a better job of designing it or communicating how to use it. And and even when I say that, I don't know if I really want to place blame on them because you know, how could they have predicted per se, you know, how people were going to, you know, abuse it? You know, maybe they couldn't. Um, but I definitely think you know, going forward in in React and the React community, that's a big area for improvement directly from the React team themselves. If they can better communicate how to use Use Effect and when to use it and what it's used for, I think we'll all be a lot better off. And I have seen progress in this area. Like if you look at the new beta React docs, they definitely um do a better job of explaining this. I think there's still room for improvement, um, but it definitely seems like they understand this problem and they are working on it, which is encouraging. Okay, okay, yeah. So, like you said in a previous episode, what was it called? Like uh you're using use effect wrong. Um, we got that. Yeah, we got that. You don't like use effect, you feel like people use it wrong, but um, you know, beyond use effect, uh, you also mentioned, you know, at the very beginning some some like struggles you had with, you know, testing like uh React components. Uh, can you elaborate on that? Like what struggles have you actually had in testing React components? Oh my goodness, what struggles have I had? Uh, all of them? I mean, testing React apps sucks. I I guess the upside is testing web-based React apps is much better than the pain the team, you know, that that built the React Native app has gone through and trying to build their automated tests, but honestly, it sucks. Like testing React apps just sucks. Um, and I feel bad saying this because like initially I was really excited for uh the testing setup on this new app, uh, this port, you know, because I'd learned some new techniques and uh found some new libraries that I thought would make testing React apps better. Like this uh React specific library for selecting React components instead of trying to always work with the rendered DOM elements from the components. And in general, Cypress, which I still think is the best testing system for React apps, has gotten better. Um, but it's still incredibly painful. I think most developers don't write any tests, or very few. And of the developers that do regularly write automated tests, most don't write very many that run directly against their React apps. And this is a real shame because I truly believe good testing speeds up and massively, massively improves the development lifecycle. Um, you know, but at the same time, I totally get why people don't test React apps very well. It's just a really cruddy experience. Um, and you know what I've come to learn just based on doing it and working with other people is I find it to be a really frustrating and terrible experience, but I seem to have a lot more experience doing it than most people. And so it's less, you know, challenging for me than it is for most people. And so it I'm sitting here thinking, like, wow, if this is so painful, even with these new techniques that I've used, for me to do it, like like nobody's gonna do this, like so few people are gonna actually write real good comprehensive tests for their React libraries and their React applications. Like it's just it's not fun, you know? It takes forever, they're brittle. Anyways, anyways, I could go on and on. Alright, all right. So pretend I'm one of those devs that writes tests but not for React. You know, can you tell me what's so bad about it? Yeah, it's I think it's a few things. Um, by far the biggest issue is that all of the testing frameworks I've seen ultimately test against DOM elements and like select against DOM elements without providing a very good connective tissue between React and those DOM elements. Um, so you kind of have to live in like these two different worlds. You're like, okay, I want to test this React component or this React page or whatever, right? Uh, but you can't like just write a selector necessarily for that React component on that page and write tests against that component. You have to be like, okay, what is this component? Well, what does it render? What is the what are the DOM elements or whatever that this outputs? And you you have to write your tests against sort of the output of your components. And I find that is you know an extra annoying step, you know, like I want to be able to just write a, you know, like let me select the you know home page component and you know, maybe inspect its props and its state, and then you know, select a component, you know, within that component that that you know, home page renders or whatever, and check to make sure it contains the right value or whatever, right? Um, that's what I want to do. That's the most natural way of writing tests, but I haven't found any framework that like actually lets you do that very easily. Like most of them you're like adding your custom test IDs to the you know output and stuff like that, and it it's a real pain. Um, a great example of this is like let's say you want to grab a reference to something, to say test its contents or to trigger a click event handler on it. Um, but then you know, React re-renders in between when your test grabs a reference to the DOM element and when you like go to you know see what's in that DOM element or to you know trigger its click event handler. And when this happens, your test will break because the DOM element is no longer there. React has replaced it with a new DOM element. Um, with Cypress, you'll see this error all the time as like a detached DOM error. And this is super frustrating because it depends on when React renders. And maybe when running on your local machine, the tests always pass and work because React rendered sooner, you know. Um, but in the CI system, the test fails 50% of the time because the system is like a little bit slower or a little bit faster and you know delays or speeds up when React renders. So then like you're not even able to reproduce the problem on your dev environment, and the tests sometimes pass in CI. It's enough to make even the most seasoned engineers give up on writing good React tests. And you know, now I have found a library, Cypress React Selector, um, that was actually made to help solve part of that problem and also solve another problem I mentioned, which is like writing selectors against React components instead of against the DOM elements output by React. And at first, this seemed to be going well. Like I was using it, I was like, oh wow, this is great. This is such a big improvement, right? Oh, but I've ran into so many issues and bugs both within that library and within its use with other libraries as well. You know, it's really gotten to the point where I think the biggest flaw in React is actually the lack of native support that it has for testing frameworks. Good systems have good tooling that enables high quality development. And React is severely lacking in this area. Alright, alright. So is it a lot easier than to write tests for the React native app? Perhaps? Oh heavens no. In fact, it's way worse. The team responsible for the automated test on the React native app have had to battle many more issues than on the you know React for DOM setup. And again, I think this points to React needing better internal support for testing. I'm excited, you know, for all the new React features that I think we'll see coming from React 18, but I'm also really disappointed it didn't include some better support for testing React apps. Like one of the biggest problems I'm running into is that with Material UI, uh, which is the component library that this project uses, you know, it I'm I'm trying to make use of the Cypress React selector uh to you know select you know actual React components and write my test that way instead of having to, you know, write selectors for DOM elements that you know end up disappearing when React re-renders or whatever. Well, Material UI basically makes it impossible to use this approach directly on any of the MUI components because of how they've like specified them or something. I haven't really tracked it down, but basically it just doesn't work. The React selector just doesn't like see them, right? And I found this issue even with components I write myself, and it it's yeah, it's really frustrating. And it really feels like to me that testing in the React ecosystem is just a complete afterthought. If it's a thought at all, and it's much worse for the React Native apps. It's actually a large reason why I generally never actually recommend React Native at all. You don't recommend people use React Native? Is that true? Like, I shouldn't we be using React for everything? I mean, come on, you know, like that that just makes sense. We're React people here, right? You know, can you tell us more about that? Yeah, exactly. I mean, I've never really been sold on React Native, but I think the testing story and third-party library integration support really seals the deal for me. And talking with the lead on the React Native app, they've said they wouldn't use React Native again if they were making the app. I think it definitely has its place. And I wouldn't say you should never use it, but you definitely give up a lot when you go to React Native. Um, beyond the testing difficulties, the biggest challenges that team has faced is with third-party library support and just sort of performance in general. Um, many libraries just don't support React Native very well or at all. And even when they do, there are often user experience sacrifices that are made. Um, React Native isn't the you know worst sort of universal system by any means. But if you want to create really high-quality user experience in your apps, then you're always going to be fighting an uphill battle if you go with React Native because it's just not actually native. It doesn't necessarily use native system iOS or Android components in the way that you would if you were writing a truly native app. And this also relates to performance, as there are unique performance characteristics of mobile apps, and it just doesn't seem like near as much work has gone into optimizing, you know, like the React libraries for the uniqueness of mobile apps with React Native. So you always end up having to spend a lot more time trying to optimize performance on a React Native app. And I think a big challenge that the team found, especially when trying to work with uh sort of more bleeding edge technologies like adding the crypto features, is that React Native and the React Native ecosystem is kind of behind in in you know versions uh relative to React in general. And so um, you know, some library gets updated or comes out, you know, for a newer version of React than the React Native ecosystem really supports. And so they would often end up having to like fork open source libraries to try to get it to work with whatever version of React Native they have to be on to you know get something to work with something else. And it always seemed like a really annoying and and really large challenge for them just trying to get all the different versions to work because React Native is just it's just a second-class citizen in the you know the React ecosystem and doesn't get you know the newest libraries right away or whatever. Fascinating. Uh so have you found the uh web version to be better than the original React Native apps? Well, yeah, but I mean it's hard to say how much of that is due to it being web. It's definitely easier to look at the original app and see the flaws in it and avoid them, you know. Um, but some things are definitely just better in web by default. I think NextJS is really good, you know, and and handles so many things for you that the team had to set up manually with React Native. Um, and like I was saying earlier, I think the flexibility that NextJS provides in terms of data loading just makes it easier to create higher quality code. And React is truly still a web-first technology, like I was just saying, and and things written for React just work better on web. So you don't need to add as many uh like hacks into the system. And I think like I haven't run into this because it's a new project still yet, but in my experience, another big difference is just the upgrade path. Um, the team you know running the React Native app, upgrading like major you know, React Native versions or React versions and library versions, um, I think has just been way more of a headache than I've experienced on the website. And and that's not to say that we don't have our upgrading challenges, but it just seems like um, you know, maybe just because not developed as much or or what? I'm not really sure why, but that team has really struggled trying to keep things working when you know they do major upgrades. Oh yeah, you know that makes sense, eh? Uh have you I'm curious though, like have you uh run into any other uh differences between the systems? Absolutely. Um, probably the other large difference is with the authentication system. Um, in the web world, the best way to do sessions is still with cookies, and a cookie-based authentication system fits in well with Next.js and their page infrastructure. Um, but the React Native app was designed around using JWTs and local storage, which you can do and use on the web, but it's definitely less secure and a worse user experience and a cookie-based approach. I know people might not agree with me on that, but they're wrong. Um so it's definitely a challenge to fit the more like web-centric authentication system into the existing backend. Honestly, I'm still not super happy with how it all turned out, but without like re-engineering the back end and the API, I'm not really sure what can be done. Um, and unfortunately, we have to integrate with this service called magic.link, um, which provides some authentication services, and I have just found it makes our authentication system overall way more complex than it should be. Authentication systems should just be simple and use off the shelf libraries 99% of the time. But 99% of the time, of course, it seems like that doesn't happen, right? Um, like really authentication systems in my experience are the number one, or at least the the first, you know, place for like security vulnerabilities, security flaws. Everyone, I don't know why, just you know, I think even even if they're trying to do things right and use, say, a service like magic.link, um, they end up wanting some feature it doesn't have, or you know, need to do something it doesn't support, or even just aren't paying attention and extend the authentication system and almost always introduce vulnerabilities. Um, and going back to the testing, magic.link just doesn't provide good testing facilities, or even any decent React support. So that was a real challenge to implement and create real reliable tests for. And and actually, in fact, you can't really create good tests for it, like they just don't support it, um, which is terrible in my opinion. Um, and and overall, I'm just really frustrated at the lack of effort put into testing, you know, within the React ecosystem in general. Like, I know it's not easy, but it's just so incredibly valuable and I think necessary. Like you look at other industries, and I think in a lot of cases, you know, not that they don't also have flaws, but I think they definitely put more effort into testing. Like I always think back to conversations with my dad in the automotive industry, and they, you know, part of the design process was designing something to be tested. And they had incredible, you know, comprehensive testing systems where they could even, you know, they would even find flaws in like the manufacturing process of like metals and you know, stuff like that. Um, and that was just part of the design process was making sure things could be tested. And I just feel like that's completely missing, especially in the React ecosystem. Oh, yeah, you know, you you make that sound pretty terrible, but uh do you have any advice for anyone listening that might want to add testing to a React app? Like you keep harping on it. Um, so what can people do? Yeah, I mean, the biggest advice I can give you beyond you know that you should do it at all, is to just be really, really persistent and determined. It will be frustrating, but it will pay off. You will run into things that don't seem to make any sense or that fail intermittently for seemingly no good reason. But if you are patient and just keep working at it and trying to, you know, understand um what is happening, you'll eventually figure it out and eventually you'll be able to create some very reliable tests. And it definitely pays to keep up to date with testing libraries. Um, they're getting better. And I even heard the upcoming version of Cypress, I think Cypress 12, will have theoretically some direct improvements for React apps. Oh, all right. That that sounds promising. Um, you know, I I would this made me think like, does it help to have like a really solid understanding of how like React and you know works internally, how React renders? You know, does does that make it easier, eh? Oh, for sure. It you know, the biggest hurdle to testing React apps is probably just a lack of understanding of what React is doing behind the scenes. So when tests break in unexpected ways, people don't really know where to look if they don't understand the internals of React well enough. And I mean that's really unfortunate. You shouldn't need to understand React like that to write good tests, but I think to some extent that's where we are currently. Like you can get by without it, um, but I think you'll save a lot of time if you have at least a basic understanding of what React is doing. Ah, that's a bummer. Um you you did write a book on the internals of React, though, right? Would that help? Yeah, for sure I did. Um Foundations of High Performance React is my book where you learn how to build your own React from scratch. Uh, you know, sort of learning how the real React is implemented along the way. I think it definitely helps me when I'm working through the test to understand what's going on underneath. You definitely don't need to buy my book though. Oh yeah, yeah. So are you uh ready to just bail on React then and go back to your Lisp cocoon? Well, I mean, you you could definitely write a React app with Lisp code already, of course, right? Lisp Lisp can do anything. Or or you know, just have the back end written in Lisp. But but no, really, I I like using React for front ends, and I'm going to stick with it for now. Even though I've complained a lot about some things, I still find it to be a really solid way to create high-quality web applications. You know, of course, as long as you don't, you know, use use effectron and you write those tests. I mean, well, I got it. Yeah. Uh, but yeah, that's all I have for you today, Thomas. Uh yeah, you know, thank you so much for uh telling us about your recent experience. Um, if people uh want to support you and the and the and the work you've been doing on the podcast, uh, what's the best way? Thanks so much, Jay, for uh putting this together and providing all these wonderful leading questions. Um yeah, definitely. If you want to support small creators like us, the best way is via our Patreon, which you can find at thereactshow.com or linked in the show notes. Um, we'd love to have you join the community there. So, yeah, what do you think? Have you ever had to port an app from React Native to web? Uh, or just port an app to React in general? Um, did you run into the same issues with testing and use effect? You know, or have you run into those in general? Um, yeah, I'd love to hear from you. And like always, if you have feedback or questions, feel free to email us at the React Show at all creek.studio. Thanks for joining us. Have a good one. Bye.