Hello, we're going to talk about effective React state and 10 years of lessons learned. I'm lucky enough to have been working in React since it was open-sourced about 10 years ago, so these are some lessons I've learned along the way.
This whole talk is going to be about state management. We've got about 20 minutes to work with here.
Let's start off with one of the things that I see most commonly in code reviews which is separating related state. This solution is quite simple which is to unify related state. Now, when I talk about related state, the concept here is to think about state where properties apply to one another, where they relate to one another. A good way to think about this is if I jump over here for a moment, I have a little app set up which is using Vite. I'm going to zoom in a little bit here, make sure everybody can read the screen, and then I have the app running over here on the left so we can see the words hi over there. Now, what if I were to come in and I were to say, create some state, and I wanted to create some state like my address and set address, and my city and set city, and my country, and so on, so you can start to see how I've created this separate state for address, for city, for country. And this is what I mean by related state. These are related because presumably when I'm actually going to go save this state to the server, what I'm going to do is send one HTTP call to send my address, and my address is composed of my street address, and my city, and my country, and so on. So what's useful here is instead of having three pieces of state, is to just have one piece of state, which might be called my address, and set address, and then this would be an object that would contain things like my city, state, country, street address, and so on. So unifying that related state has a couple of benefits. One benefit is, notice that I have one use-state call here rather than two, three, four, five, six. Could be a high number. The other thing is, when I go to initialize this, it's simpler because think about what might happen. Imagine this is a form that is going to be populated with data from the server. So I'm gonna go fetch some data via HTTP, and then I'm gonna put that data into state. Well, if I'm going to do that, it's easier to take the object that I get back from the server and put it into one piece of state than to split it up into separate pieces of state. The other reason this is useful is, when I go to send this back to the server, presumably, I'm going to want to send one object to the server, not a bunch of separate HTTP requests. So when I go to send this back, it's also simpler. Excuse me. Okay, so that's the first lesson, just grouping-related state.
Another place where you'll commonly see this might be where somebody chooses to create state like for user information, and I might have first name and last name and all these other pieces of data that relate to the user, maybe their age and their pets, who knows, whatever information we're happening, where you happen to collect. Again, group this into a user object, we have one piece of state, it's easier to hydrate and easier to send. So that is the first lesson that I've learned.
The second one that I've learned is, watch out for when we make multiple use state calls in a row. And when we see this sort of thing, it's often a sign that we should either unify those set states together by unifying the state, much like what I just showed earlier, or it could also be a sign that we should consider using UseReducer. Now, I will say, I just recently did a poll on Twitter and it surprised me because about half of React developers have literally never used UseReducer, and this was thousands of people that replied to my poll, and then there's a fair amount that say that they virtually never use it.
Comments